1 package Memories::Photo;
3 use Carp qw(cluck confess);
4 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
6 use constant PAGER_SYNTAX => "LimitXY";
7 __PACKAGE__->columns(Essential => qw(id title uploader uploaded x y));
8 __PACKAGE__->untaint_columns(printable => [qw/title/]);
9 __PACKAGE__->columns(TEMP => qw/exif_object/);
10 __PACKAGE__->set_sql(recent => q{
13 ORDER BY uploaded DESC
17 __PACKAGE__->has_many(comments => "Memories::Comment");
18 __PACKAGE__->has_a(uploader => "Memories::User");
19 __PACKAGE__->has_a(uploaded => "Time::Piece",
20 inflate => sub { Time::Piece->strptime(shift, "%Y-%m-%d %H:%M:%S") },
21 deflate => 'datetime',
24 sub do_upload :Exported {
26 my %upload = $r->upload("photo");
28 # XXX Stop anonymous uploads!
29 my $photo = $self->create({
31 uploaded => Time::Piece->new(),
32 title => ($r->params->{title} || $upload{filename})
36 if (!open OUT, ">". $photo->path("file")) {
37 die "Can't write ".$photo->path("file")." because $!";
39 # XXX Check it's a JPEG, etc.
41 print OUT $upload{content};
43 my ($x, $y) = dim(image_info($photo->path));
44 $photo->x($x); $photo->y($y);
50 $photo->add_tags($r->{params}{tags});
51 Memories->zap_cache();
53 # Add system tags here
54 my $tag = "date:".$photo->shot->ymd;
55 $photo->add_to_system_tags({tag => Memories::SystemTag->find_or_create({name =>$tag}) });
57 # Set it up to go again
58 $r->objects([$photo]);
60 $r->message("Thanks for the upload! ".
61 ($r->{params}{tags} ? ""
62 : "Don't forget to <a href=\"?".$r->config->uri_base."photo/view/".$photo->id."?active=tagedit\">tag your photo</a>"
67 sub upload :Exported {}
69 use Class::DBI::Plugin::Pager;
70 use Class::DBI::Plugin::AbstractCount;
72 sub recent :Exported {
74 my $page = $r->params->{page} || 1;
75 my $pager = $self->pager(
76 per_page => Memories->config->{photos_per_page},
78 syntax => PAGER_SYNTAX,
79 order_by => "uploaded desc"
81 $r->objects([$pager->retrieve_all ]);
82 $r->{template_args}{pager} = $pager;
85 sub add_comment :Exported {
86 my ($self, $r, $photo) = @_;
88 $r->objects->[0]->add_to_comments({
89 name => $r->params->{name},
90 content => $r->params->{content}
98 use Cache::MemoryCache;
99 use Image::Info qw(dim image_info);
101 my $cache = new Cache::MemoryCache( { 'namespace' => 'MemoriesInfo' });
105 my $orient = $self->exif_info->{EXIF}->{Orientation};
106 return if $orient !~/Rotate (\d+)/i;
108 my $img = Image::Imlib2->load($self->path("file"));
109 $img->image_orientate($steps);
110 $img->save($self->path("file"));
115 my $info = $self->exif_object;
116 return $info if $info;
117 # Get it from the Cache
118 if (!($info = $cache->get($self->id))) {
120 $info = $self->_exif_info;
121 $cache->set($self->id, $info);
123 $self->exif_object($info);
128 my $exifTool = new Image::ExifTool;
129 $exifTool->Options(Group0 => ['EXIF', 'MakerNotes', 'Composite']);
130 my $info = $exifTool->ImageInfo(shift->path);
132 foreach my $tag ($exifTool->GetFoundTags('Group0')) {
133 my $group = $exifTool->GetGroup($tag);
134 my $val = $info->{$tag};
135 next if ref $val eq 'SCALAR';
136 next if $val =~ /^[0\s]*$/;
137 $hash->{$group}->{$exifTool->GetDescription($tag)} = $val;
143 sub dimensions { join "x", $_[0]->x, $_[0]->y }
146 my ($self, $size) = @_;
147 return 1 if $size eq "full";
148 my ($w, $h) = ($self->x, $self->y);
149 my ($w2, $h2) = split /x/, $size;
150 return 1 if $w > $w2 or $h > $h2;
154 sub sized_url { # Use this rather than ->path from TT
155 my ($self, $size) = @_;
156 my $url = Memories->config->{data_store_external};
157 my $resized = Memories->config->{sizes}->[$size];
158 if (!$resized) { cluck "Asked for crazy size $size"; return; }
159 if ($resized eq "full") { return $self->path("url") }
160 $self->scale($resized)
161 unless -e $self->path( file => $resized );
162 return $self->path(url => $resized);
166 my ($self, $is_url, $scale) = @_;
168 Memories->config->{$is_url eq "url" ? "data_store_external" : "data_store" };
169 if ($scale) { $path .= "$scale/" }
170 # Make dir if it doesn't exist, save trouble later
172 if ($is_url ne "url") {mkpath($path);}
173 $path .= $self->id.".".$self->format;
177 sub thumb_url { shift->path(url => Memories->config->{thumb_size}); }
178 sub make_thumb { shift->scale(Memories->config->{thumb_size}, 1); }
182 my ($self, $scale, $swap) = @_;
183 my ($x, $y) = split /x/, $scale;
184 # Find out smaller axis
185 my ($cur_x, $cur_y) = ($self->x, $self->y);
187 if ($cur_x < $cur_y) { $y = 0 } else { $x = 0 }
189 if ($cur_x > $cur_y) { $y = 0 } else { $x = 0 }
191 my $img = Image::Imlib2->load($self->path("file"));
193 cluck "Couldn't open image file ".$self->path("file");
196 $img = $img->create_scaled_image($x, $y);
197 $img->image_set_format("jpeg");
198 my $file = $self->path( file => $scale );
201 cluck "Couldn't write image file $file ($!)";
206 use Text::Balanced qw(extract_multiple extract_quotelike);
207 sub edit_tags :Exported {
209 my $photo = $r->objects->[0];
210 my %params = %{$r->params};
212 next unless /delete_(\d+)/;
213 my $tagging = Memories::Tagging->retrieve($1) or next;
214 next unless $tagging->photo->id == $photo->id;
217 $photo->add_tags($params{newtags});
218 $r->template("view");
222 my ($photo, $tagstring) = @_;
224 for my $tag (map { s/^"|"$//g; $_} extract_multiple(lc $tagstring, [ \&extract_quotelike, qr/([^\s]+)/ ], undef,1)) {
225 $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) })
231 my $exif = $self->exif_info->{EXIF};
233 grep {$_ and /[^ 0:]/}
234 ($exif->{ 'Shooting Date/Time' },
235 $exif->{ 'Date/Time Of Digitization' },
236 $exif->{ 'Date/Time Of Last Modification' });
237 if (!$dt) { return $self->uploaded }
238 return Time::Piece->strptime($dt, "%Y:%m:%d %T") || $self->uploaded;