From: Martin Mares Date: Mon, 3 Feb 2014 00:20:23 +0000 (+0100) Subject: Caching of EXIF position and time X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=217631e9b9c70c7495f57b02c8d6b49ad0d4ec82;p=gallery.git Caching of EXIF position and time --- diff --git a/gal/FORMAT b/gal/FORMAT index e0f776a..a7a4463 100644 --- a/gal/FORMAT +++ b/gal/FORMAT @@ -26,6 +26,10 @@ $meta->{photo}->{$identifier} is a hash of: w0, h0 width and height of original image title photo title fmt photo format (png/jpg; defaults to jpg) + lat geographic latitude in degrees north of equator + lon geographic longitude in degrees east of Greenwich + alt geographic altitude in meters + t time when the photo was taken (2014-01-25 09:40:12) The rest is present in cache.meta only: diff --git a/gal/UCW/Gallery.pm b/gal/UCW/Gallery.pm index c2afc82..045b41f 100644 --- a/gal/UCW/Gallery.pm +++ b/gal/UCW/Gallery.pm @@ -1,5 +1,5 @@ # Simple Photo Gallery -# (c) 2003--2012 Martin Mares +# (c) 2003--2014 Martin Mares package UCW::Gallery; @@ -30,6 +30,7 @@ sub new($) { PhotoMaxHeight => 1024, ThumbFormats => [ "114x94" ], # Built-in themes use the first size, # but more can be generated + CacheExif => 0, # Cache selected EXIF meta-data # Titles and navigation Title => 'An Unnamed Gallery', diff --git a/gal/bin/gal-gen b/gal/bin/gal-gen index 22a38ba..0c7921a 100755 --- a/gal/bin/gal-gen +++ b/gal/bin/gal-gen @@ -6,6 +6,7 @@ use strict; use warnings; use UCW::Gallery; +use Image::EXIF; use Image::Magick; use IO::Handle; use File::Spec; @@ -64,6 +65,70 @@ sub get_meta_basic($$$) { $m->{h} = $h; } +sub get_meta_exif($$) { + my ($f, $m) = @_; + $gal->get('CacheExif') or return; + + my $e = new Image::EXIF($f->{orig}); + my $i = $e->get_all_info(); + if ($e->error) { + print "[EXIF error: ", $e->error, "] "; + return; + } + # use Data::Dumper; print Dumper($i); + + my $lat = $i->{image}->{'Latitude'}; + if ($lat) { + if ($lat =~ m{^([NS]) (\d+)\xb0 ([0-9.]+)'$}) { + $lat = $2 + $3/60; + $lat = -$lat if $1 eq 'S'; + $lat = sprintf "%.6f", $lat; + } else { + print "[EXIF: unable to parse latitude $lat] "; + $lat = undef; + } + } + + my $lon = $i->{image}->{'Longitude'}; + if ($lon) { + if ($lon =~ m{^([WE]) (\d+)\xb0 ([0-9.]+)'$}) { + $lon = $2 + $3/60; + $lon = -$lon if $1 eq 'W'; + $lon = sprintf "%.6f", $lon; + } else { + print "[EXIF: unable to parse longitude $lon] "; + $lon = undef; + } + } + + my $alt = $i->{image}->{'Altitude'}; + if ($alt) { + if ($alt =~ m{^([0-9.]+) m$}) { + $alt = $1; + } else { + print "[EXIF: unable to parse altitude $alt] "; + $alt = undef; + } + } + + # printf "[GEO: lat=%s lon=%s alt=%s] ", $lat // '?', $lon // '?', $alt // '?'; + if ($lat && $lon) { + $m->{lat} = $lat; + $m->{lon} = $lon; + } + $m->{alt} = $alt if $alt; + + my $time = $i->{image}->{'Image Created'}; + if ($time) { + if ($time =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}:\d{2})$}) { + $m->{t} = "$1-$2-$3 $4"; + # print "[TIME: ", $m->{t}, "] "; + } else { + print "[EXIF: unable to parse time $time] "; + } + } +} + sub generate_photo($$$) { my ($f, $m, $p) = @_; @@ -118,6 +183,7 @@ for my $f (@$orig_list) { my $p = new Image::Magick; get_meta_basic($f, $m, $p); + get_meta_exif($f, $m); my $om = $old_meta->{photo}->{$id}; if ($om &&