1 package Memories::Photo;
3 use Carp qw(cluck confess);
4 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
7 use constant PAGER_SYNTAX => "LimitXY";
8 __PACKAGE__->columns(Essential => qw(id title uploader uploaded x y rating rated hit_count));
9 __PACKAGE__->untaint_columns(printable => [qw/title/]);
10 __PACKAGE__->columns(TEMP => qw/exif_object/);
11 __PACKAGE__->set_sql(recent => q{
14 ORDER BY uploaded DESC
17 __PACKAGE__->set_sql(popular => q{
20 ORDER BY hit_count DESC
24 __PACKAGE__->has_many(comments => "Memories::Comment");
25 __PACKAGE__->has_a(uploader => "Memories::User");
26 __PACKAGE__->has_a(uploaded => "Time::Piece",
27 inflate => sub { Time::Piece->strptime(shift, "%Y-%m-%d %H:%M:%S") },
28 deflate => 'datetime',
31 sub do_upload :Exported {
33 my %upload = $r->upload("photo");
35 # XXX Stop anonymous uploads!
36 my $photo = $self->create({
38 uploaded => Time::Piece->new(),
39 title => $r->params->{title},
42 rated => 0, # Oh, the potential for divide by zero errors...
46 if (!open OUT, ">". $photo->path("file")) {
47 die "Can't write ".$photo->path("file")." because $!";
49 # XXX Check it's a JPEG, etc.
51 print OUT $upload{content};
53 my ($x, $y) = dim(image_info($photo->path));
54 $photo->x($x); $photo->y($y);
59 $photo->title($photo->title_exif || $upload{filename});
63 $r->{params}{tags} ||= join " ", map { qq{"$_"} } $photo->tags_exif;
64 $photo->add_tags($r->{params}{tags});
65 $photo->add_to_imageseek_library;
66 Memories->zap_cache();
68 # Add system tags here
69 my $tag = "date:".$photo->shot->ymd;
70 $photo->add_to_system_tags({tag => Memories::SystemTag->find_or_create({name =>$tag}) });
72 # Set it up to go again
73 $r->objects([$photo]);
75 $r->message("Thanks for the upload! ".
76 ($r->{params}{tags} ? ""
77 : "Don't forget to <a href=\"?".$r->config->uri_base."photo/view/".$photo->id."?active=tagedit\">tag your photo</a>"
84 $self->rated or return 0;
85 int($self->rating/$self->rated*10)/10;
88 sub add_rating :Exported {
90 my $photo = $r->{objects}[0];
91 my $delta = $r->{params}{rating};
92 if ($delta < 0 or $delta > 5) { return; } # Scammer
94 $photo->rating($photo->rating() + $delta);
95 $photo->rated($photo->rated() + 1);
96 $r->output(""); # Only used by ajax
101 my $photo = $r->{objects}[0];
102 $photo->hit_count($photo->hit_count()+1);
103 if ($r->{session}{last_search}) {
104 # This is slightly inefficient
105 my @search = split/,/, $r->{session}{last_search};
107 for my $i (0..$#search) {
108 next unless $photo->id == $search[$i];
111 return unless $found > -1;
112 $r->{template_args}{next} = $self->retrieve($search[$found+1])
113 if $found+1 <= $#search;
114 $r->{template_args}{prev} = $self->retrieve($search[$found-1])
118 sub upload :Exported {}
119 sub exif :Exported {}
120 sub comment :Exported {}
121 sub tagedit :Exported {}
122 sub similar :Exported {}
124 use Class::DBI::Plugin::Pager;
125 use Class::DBI::Plugin::AbstractCount;
127 sub view_paged_ordered {
128 my ($self, $r, $how) = @_;
129 my $page = $r->params->{page} || 1;
130 my $pager = $self->pager(
131 per_page => Memories->config->{photos_per_page},
133 syntax => PAGER_SYNTAX,
136 $r->objects([$pager->retrieve_all ]);
137 $r->{template_args}{pager} = $pager;
139 $r->template("paged"); # Set the what using the action name
142 sub recent :Exported {
144 $self->view_paged_ordered($r, "uploaded desc");
147 sub popular :Exported {
149 $self->view_paged_ordered($r, "hit_count desc");
152 sub add_comment :Exported {
153 my ($self, $r, $photo) = @_;
154 $r->template("view");
155 $r->objects->[0]->add_to_comments({
156 name => $r->params->{name},
157 content => $r->params->{content}
165 use Cache::MemoryCache;
166 use Image::Info qw(dim image_info);
168 my $cache = new Cache::MemoryCache( { 'namespace' => 'MemoriesInfo' });
170 sub add_to_imageseek_library {
172 Image::Seek::cleardb();
173 my $img = Image::Imlib2->load($self->path("file"));
175 Image::Seek::add_image($img, $self->id);
176 # Merge this new one into the main database; there is a bit of a
177 # race condition here. XXX
178 Image::Seek::loaddb(Memories->config->{image_seek});
179 Image::Seek::savedb(Memories->config->{image_seek});
182 sub recommended_tags {
184 my %tags = map { $_->name => $_ }
186 $self->find_similar(3);
191 my ($self, $count) = @_;
192 Image::Seek::cleardb();
193 Image::Seek::loaddb(Memories->config->{image_seek});
194 my @res = map {$_->[0] } Image::Seek::query_id($self->id, $count);
196 map { $self->retrieve($_) } @res;
201 my $orient = $self->exif_info->{EXIF}->{Orientation};
202 return if $orient !~/Rotate (\d+)/i;
204 my $img = Image::Imlib2->load($self->path("file"));
205 $img->image_orientate($steps);
206 $img->save($self->path("file"));
211 my $info = $self->exif_object;
212 return $info if $info;
213 # Get it from the Cache
214 if (!($info = $cache->get($self->id))) {
216 $info = $self->_exif_info;
217 $cache->set($self->id, $info);
219 $self->exif_object($info);
223 my %banned_tags = map { $_ => 1 }
224 qw( CodedCharacterSet ApplicationRecordVersion );
227 my $exifTool = new Image::ExifTool;
228 $exifTool->Options(Group0 => ['IPTC', 'EXIF', 'XMP', 'MakerNotes', 'Composite']);
229 my $info = $exifTool->ImageInfo(shift->path);
231 foreach my $tag ($exifTool->GetFoundTags('Group0')) {
232 next if $banned_tags{$tag};
233 my $group = $exifTool->GetGroup($tag);
234 my $val = $info->{$tag};
235 next if ref $val eq 'SCALAR';
236 next if $val =~ /^[0\s]*$/ or $val =~ /^nil$/;
237 $hash->{$group}->{$exifTool->GetDescription($tag)} = $val;
243 sub dimensions { join "x", $_[0]->x, $_[0]->y }
246 my ($self, $size) = @_;
247 return 1 if $size eq "full";
248 my ($w, $h) = ($self->x, $self->y);
249 my ($w2, $h2) = split /x/, $size;
250 return 1 if $w > $w2 or $h > $h2;
254 sub sized_url { # Use this rather than ->path from TT
255 my ($self, $size) = @_;
256 my $url = Memories->config->{data_store_external};
257 my $resized = Memories->config->{sizes}->[$size];
258 if (!$resized) { cluck "Asked for crazy size $size"; return; }
259 if ($resized eq "full") { return $self->path("url") }
260 $self->scale($resized)
261 unless -e $self->path( file => $resized );
262 return $self->path(url => $resized);
266 my ($self, $is_url, $scale) = @_;
268 Memories->config->{$is_url eq "url" ? "data_store_external" : "data_store" };
269 if ($scale) { $path .= "$scale/" }
270 # Make dir if it doesn't exist, save trouble later
272 if ($is_url ne "url") {mkpath($path);}
273 $path .= $self->id.".".$self->format;
277 sub thumb_url { shift->path(url => Memories->config->{thumb_size}); }
278 sub make_thumb { shift->scale(Memories->config->{thumb_size}, 1); }
282 my ($self, $scale, $swap) = @_;
283 my ($x, $y) = split /x/, $scale;
284 # Find out smaller axis
285 my ($cur_x, $cur_y) = ($self->x, $self->y);
287 if ($cur_x < $cur_y) { $y = 0 } else { $x = 0 }
289 if ($cur_x > $cur_y) { $y = 0 } else { $x = 0 }
291 my $img = Image::Imlib2->load($self->path("file"));
293 cluck "Couldn't open image file ".$self->path("file");
296 $img = $img->create_scaled_image($x, $y);
297 $img->image_set_format("jpeg");
298 my $file = $self->path( file => $scale );
301 cluck "Couldn't write image file $file ($!)";
306 sub edit_tags :Exported {
308 my $photo = $r->objects->[0];
309 my %params = %{$r->params};
311 next unless /delete_(\d+)/;
312 my $tagging = Memories::Tagging->retrieve($1) or next;
313 next unless $tagging->photo->id == $photo->id;
316 $photo->add_tags($params{newtags});
317 $r->template("view");
321 my ($photo, $tagstring) = @_;
323 for my $tag (Tagtools->separate_tags($tagstring)) {
324 $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) })
328 # Work out some common properties from a set of potential photo metadata
330 sub _grovel_metadata {
331 my ($self, @tags) = @_;
332 my %md = map {%$_} values %{$self->exif_info};
334 if ($md{$_} and $md{$_} =~/[^ 0:]/) { return $md{$_} }
341 my $dt = $self->_grovel_metadata(
342 'Shooting Date/Time',
343 'Date/Time Of Digitization',
344 'Date/Time Of Last Modification'
346 if (!$dt) { return $self->uploaded }
347 return Time::Piece->strptime($dt, "%Y:%m:%d %T") || $self->uploaded;
351 shift->_grovel_metadata(
352 'Description', 'Image Description', 'Caption-Abstract'
356 sub title_exif { shift->_grovel_metadata( 'Headline', 'Title'); }
357 sub license { shift->_grovel_metadata( 'Rights Usage Terms', 'Usage Terms' ) }
358 sub copyright { shift->_grovel_metadata( 'Rights', 'Copyright', 'Copyright Notice') }
360 # This one's slightly different since we want everything we can get...
363 my %md = map {%$_} values %{$self->exif_info};
365 map { s/\s+/-/g; lc $_ => 1 }
366 map { split /\s*,\s*/, $md{$_}}
367 grep {$md{$_} and $md{$_} =~/[^ 0:]/}
368 (qw(Keywords Subject City State Location Country Province-State),
369 'Transmission Reference', 'Intellectual Genre',
370 'Country-Primary Location Name'