]> mj.ucw.cz Git - gallery.git/commitdiff
Caching of EXIF position and time
authorMartin Mares <mj@ucw.cz>
Mon, 3 Feb 2014 00:20:23 +0000 (01:20 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:17 +0000 (21:14 +0100)
gal/FORMAT
gal/UCW/Gallery.pm
gal/bin/gal-gen

index e0f776a4b3db888b748189514c1493129f2dcdbd..a7a4463e37e157a13fe496a48103c5cf3455a95a 100644 (file)
@@ -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:
 
index c2afc82a57a5bb6f21f62bd15b0e326e0898acea..045b41f94f6e047c1a64c3c90b03d95e99e0d4f2 100644 (file)
@@ -1,5 +1,5 @@
 # Simple Photo Gallery
-# (c) 2003--2012 Martin Mares <mj@ucw.cz>
+# (c) 2003--2014 Martin Mares <mj@ucw.cz>
 
 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',
index 22a38baa51aedf8b06e02c431537ea61aeaf487b..0c7921a84b0b910e28bb13f050ef2f51944e8c11 100755 (executable)
@@ -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 &&