]> mj.ucw.cz Git - gallery.git/blob - bin/gal-gen
1ee10b04749fd47b09b7e4cd31a885edded68d18
[gallery.git] / bin / gal-gen
1 #!/usr/bin/perl
2 # UCW Gallery: Generate published photos
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 use UCW::Gallery;
9 use Image::EXIF;
10 use Image::Magick;
11 use IO::Handle;
12 use File::Spec;
13 use File::Path;
14
15 STDOUT->autoflush(1);
16
17 my $gal = UCW::Gallery->load_config;
18
19 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
20
21 my $photo_dir = $gal->get('PhotoDir');
22 if (-d $photo_dir) {
23         print "Using existing output directory: $photo_dir\n";
24 } else {
25         print "Creating output directory: $photo_dir\n";
26         File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
27 }
28
29 my $photo_meta = $gal->photo_meta_name;
30 my $old_meta = {};
31 if (-f $photo_meta) {
32         print "Reading old meta-data\n";
33         $old_meta = $gal->read_meta($photo_meta);
34         # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
35 }
36 my $meta = { 'photo' => {} };
37
38 sub get_meta_basic($$$) {
39         my ($f, $m, $p) = @_;
40         my $rotate = $f->{orientation};
41
42         my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($f->{orig}) or die "Error reading " . $f->{orig} . "\n";
43         print "${orig_w}x${orig_h} ";
44
45         my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
46         my ($w, $h) = ($w0, $h0);
47         if ($w > $gal->get('PhotoMaxWidth')) {
48                 my $s = $gal->get('PhotoMaxWidth') / $w;
49                 $w = $w * $s;
50                 $h = $h * $s;
51         }
52         if ($h > $gal->get('PhotoMaxHeight')) {
53                 my $s = $gal->get('PhotoMaxHeight') / $h;
54                 $w = $w * $s;
55                 $h = $h * $s;
56         }
57         $w = int($w + .5);
58         $h = int($h + .5);
59         
60         $m->{o} = $rotate;
61         for my $k (keys %UCW::Gallery::list_attrs) {
62                 next if $UCW::Gallery::list_attrs{$k} < 2;
63                 $m->{$k} = $f->{$k} if defined $f->{$k};
64         }
65         $m->{w0} = $w0;
66         $m->{h0} = $h0;
67         $m->{w} = $w;
68         $m->{h} = $h;
69 }
70
71 sub parse_geo($) {
72         my ($g) = @_;
73         defined $g or return;
74         if ($g =~ m{^([NEWS]) (\d+)\xb0 ([0-9.]+)'$}) {
75                 $g = $2 + $3/60;
76                 $g = -$g if $1 eq 'W' || $1 eq 'S';
77         } elsif ($g =~ m{^([NEWS]) (\d+)\xb0 (\d+)' ([0-9.]+)$}) {
78                 $g = $2 + $3/60 + $4/3600;
79                 $g = -$g if $1 eq 'W' || $1 eq 'S';
80         } else {
81                 print "[EXIF: unable to parse coordinate $g] ";
82                 return;
83         }
84         return sprintf "%.6f", $g;
85 }
86
87 sub get_meta_exif($$) {
88         my ($f, $m) = @_;
89         $gal->get('CacheExif') or return;
90
91         my $e = new Image::EXIF($f->{orig});
92         my $i = $e->get_all_info();
93         if ($e->error) {
94                 print "[EXIF error: ", $e->error, "] ";
95                 return;
96         }
97         # use Data::Dumper; print Dumper($i);
98
99         my $lat = parse_geo($i->{image}->{'Latitude'});
100         my $lon = parse_geo($i->{image}->{'Longitude'});
101
102         my $alt = $i->{image}->{'Altitude'};
103         if ($alt) {
104                 if ($alt =~ m{^([0-9.]+) m$}) {
105                         $alt = $1;
106                 } else {
107                         print "[EXIF: unable to parse altitude $alt] ";
108                         $alt = undef;
109                 }
110         }
111
112         # printf "[GEO: lat=%s lon=%s alt=%s] ", $lat // '?', $lon // '?', $alt // '?';
113         if ($lat && $lon) {
114                 $m->{lat} //= $lat;
115                 $m->{lon} //= $lon;
116         }
117         $m->{alt} //= $alt if $alt;
118
119         my $time = $i->{image}->{'Image Created'};
120         if ($time) {
121                 if ($time =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}:\d{2})$}) {
122                         $m->{t} //= "$1-$2-$3 $4";
123                         # print "[TIME: ", $m->{t}, "] ";
124                 } else {
125                         print "[EXIF: unable to parse time $time] ";
126                 }
127         }
128 }
129
130 sub generate_photo($$$) {
131         my ($f, $m, $p) = @_;
132
133         my $e;
134         $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
135         $p->Strip;
136         $p->SetAttribute(quality=>90);
137
138         my $xfrm = $m->{xf};
139         if ($xfrm =~ /s/) {
140                 print "-> sharpen ";
141                 $p->Sharpen(1);
142         }
143         if ($xfrm =~ /h/) {
144                 print "-> equalize ";
145                 $p->Equalize();
146         }
147         if ($xfrm =~ /n/) {
148                 print "-> normalize ";
149                 $p->Normalize();
150         }
151
152         my $rotate = $m->{o};
153         my $rot = 0;
154         if ($rotate eq "l") { $rot = 270; }
155         elsif ($rotate eq "r") { $rot = 90; }
156         elsif ($rotate eq "u") { $rot = 180; }
157         if ($rot) {
158                 print "-> rotate $rot ";
159                 $p->Rotate(degrees=>$rot);
160         }
161
162         my ($w, $h) = ($m->{w}, $m->{h});
163         if ($w != $m->{w0} || $h != $m->{h0}) {
164                 print "-> ${w}x${h} ";
165                 $p->Resize(width=>$w, height=>$h);
166         }
167
168         my $photo = $gal->photo_name($m, $f->{id});
169         my $tmp = "$photo.new";
170         $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
171         rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
172 }
173
174 for my $f (@$orig_list) {
175         my $id = $f->{id};
176         print "$id: ";
177
178         my $m = { };
179         $meta->{photo}->{$id} = $m;
180         $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
181
182         my $p = new Image::Magick;
183         get_meta_basic($f, $m, $p);
184         get_meta_exif($f, $m);
185
186         my $om = $old_meta->{photo}->{$id};
187         if ($om &&
188             $om->{o} eq $m->{o} &&
189             $om->{xf} eq $m->{xf} &&
190             $om->{w} eq $m->{w} &&
191             $om->{h} eq $m->{h}) {
192                 print "... uptodate\n";
193                 next;
194         }
195
196         generate_photo($f, $m, $p);
197         print "... OK\n";
198 }
199
200 print "Cleaning up stale files\n";
201 for my $f (<$photo_dir/*.jpg>) {
202         my ($vv, $dd, $id) = File::Spec->splitpath($f);
203         $id =~ s{\..*$}{};
204         unless (defined $meta->{photo}->{$id}) {
205                 print "$id: removing\n";
206                 unlink $f;
207         }
208 }
209
210 print "Writing meta-data\n";
211 $gal->write_meta($photo_meta, $meta);
212 exit 0;