File: Photo.pm

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (265 lines) | stat: -rw-r--r-- 6,374 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package MojoMojo::Schema::Result::Photo;

use strict;
use warnings;

use parent qw/MojoMojo::Schema::Base::Result/;

use DateTime;
use Image::ExifTool;
use Image::Math::Constrain;
my $exif = Image::ExifTool->new();

__PACKAGE__->load_components(
    qw/DateTime::Epoch TimeStamp Ordered Core/);

__PACKAGE__->position_column("position");
__PACKAGE__->table("photo");
__PACKAGE__->add_columns(
    "id",
    {
        data_type         => "INTEGER",
        is_nullable       => 0,
        size              => undef,
        is_auto_increment => 1
    },
    "position",
    { data_type => "INTEGER", is_nullable => 0, size => undef },
    "title",
    { data_type => "TEXT", is_nullable => 0, size => undef },
    "description",
    { data_type => "TEXT", is_nullable => 1, size => undef },
    "camera",
    { data_type => "TEXT", is_nullable => 1, size => undef },
    "taken",
    {
        data_type                 => "INTEGER",
        is_nullable               => 1,
        size                      => undef,
        default_value             => undef,
        inflate_datetime          => 'epoch',
        datetime_undef_if_invalid => 1,
    },
    "iso",
    { data_type => "INTEGER", is_nullable => 1, size => undef },
    "lens",
    { data_type => "TEXT", is_nullable => 1, size => undef },
    "aperture",
    { data_type => "TEXT", is_nullable => 1, size => undef },
    "flash",
    { data_type => "TEXT", is_nullable => 1, size => undef },
    "height",
    { data_type => "INT", is_nullable => 1, size => undef },
    "width",
    { data_type => "INT", is_nullable => 1, size => undef },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
    "tags",
    "MojoMojo::Schema::Result::Tag",
    { "foreign.photo" => "self.id" }
);
__PACKAGE__->has_many(
    "comments",
    "MojoMojo::Schema::Result::Comment",
    { "foreign.picture" => "self.id" }
);
__PACKAGE__->has_one( "attachment", "MojoMojo::Schema::Result::Attachment" );

=head1 NAME

MojoMojo::Schema::Result::Photo - store photos

=head1 METHODS

=cut

=head2 extract_exif

Extracts EXIF information from a given Attachment and
populates the Photo object.

=cut

sub extract_exif {
    my ( $self, $att ) = @_;
    my $info = $exif->ImageInfo( $att->filename );
    $self->camera( $info->{'Model'} );
    $self->lens( $info->{'FocalLength'} );
    $self->iso( $info->{'ISO'} );
    $self->aperture( $info->{'Aperture'} );
    $self->flash( $info->{'Flash'} );
    $self->description( $info->{'UserComment'} );
    $self->taken( $self->exif2datetime( $info->{'DateTimeOriginal'} ) );
}

=head2 exif2datetime datetime

Creates a L<DateTime> object from an EXIF timestamp.

=cut

sub exif2datetime {
    my ( $self, $datetime ) = @_;
    return undef unless $datetime;
    my ( $date, $time ) = split( ' ', $datetime );
    my ( $y, $M, $d ) = split ':', $date;
    my ( $h, $m, $s ) = split ':', $time;
    my $dto;
    eval {
        $dto = DateTime->new(
            year   => $y,
            month  => $M,
            day    => $d,
            hour   => $h,
            minute => $m,
            second => $s
        );
    };
    return $dto;
}

=head2 prev_by_tag <tag>

Return previous image when browsing by the given tag.

=cut

sub prev_by_tag {
    my ( $self, $tag ) = @_;
    return $self->result_source->resultset->search(
        { 'me.id' => { '>', $self->id },
          'tags.tag' => $tag 
        },
        { order_by => 'taken',
          join => [qw/tags/],rows=>1
        }
    )->next;
}

=head2 next_by_tag <tag>

Return the next image when browsing by the given tag.

=cut

sub next_by_tag {
    my ( $self, $tag ) = @_;
    return $self->result_source->resultset->search(
        { 'me.id' => { '<', $self->id },
          'tags.tag' => $tag
        },
        { order_by => 'taken DESC',
          join => [qw/tags/], rows => 1 }
    )->next;
}

=head2 others_tags <user>

Tags other users have given to this photo.

=cut

sub others_tags {
    my ( $self, $user ) = @_;
    my (@tags) = $self->related_resultset('tags')->search(
        {
            photo  => $self->id,
            person => { '!=', $user },
        },
        {
            select     => [ 'me.tag', 'count(me.tag) AS refcount' ],
            as         => [ 'tag',    'refcount' ],
            'group_by' => ['me.tag'],
            'order_by' => 'refcount',
        }
    );
    return @tags;
}

=head2 user_tags <user>

Tags this user has given to this photo.

=cut

sub user_tags {
    my ( $self, $user ) = @_;
    my (@tags) = $self->related_resultset('tags')->search(
        {
            photo  => $self->id,
            person => $user,
        },
        { 'order_by' => ['me.tag'] }
    );
    return @tags;
}

=head2 make_inline

Create a resized version of a photo suitable for inline usage.

=cut

sub make_inline {
    my ($self) = shift;
    my $img    = Imager->new();
    my $att    = $self->attachment;
    $img->open( file => $att->filename ) or die $img->errstr;
    my $constrain = Image::Math::Constrain->new( 800, 600 );
    my $image = $img->scale( constrain => $constrain );

    $image->write( file => $att->filename . '.inline', type => 'jpeg' )
      or die $img->errstr;
}

=head2 make_thumb

Create a thumbnail version of a photo, for gallery views and linking to pages.

=cut

sub make_thumb {
    my ($self) = shift;
    my $img    = Imager->new();
    my $att    = $self->attachment;
    $img->open( file => $att->filename ) or die $img->errstr;
    my $h = $img->getheight;
    my $w = $img->getwidth;
    my ( $image, $result );
    if ( $h > $w ) {
        $image  = $img->scale( xpixels => 80 );
        $h      = $image->getheight;
        $result = $image->crop(
            top => int( ( $h - 80 ) / 2 ),
            left   => 0,
            width  => 80,
            height => 80
        );
    }
    else {
        $image  = $img->scale( ypixels => 80 );
        $w      = $image->getwidth;
        $result = $image->crop(
            left => int( ( $w - 80 ) / 2 ),
            top => 0,
            width  => 80,
            height => 80
        );
    }
    $result->write( file => $att->filename . '.thumb', type => 'jpeg' )
      or die $img->errstr;
}

=head1 AUTHOR

Marcus Ramberg <mramberg@cpan.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;