2 # UCW Gallery: Generate published photos
3 # (c) 2004--2014 Martin Mares <mj@ucw.cz>
17 my $gal = UCW::Gallery->load_config;
19 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
21 my $photo_dir = $gal->get('PhotoDir');
23 print "Using existing output directory: $photo_dir\n";
25 print "Creating output directory: $photo_dir\n";
26 File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
29 my $photo_meta = $gal->photo_meta_name;
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";
36 my $meta = { 'photo' => {} };
38 sub get_meta_basic($$$) {
40 my $rotate = $f->{orientation};
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} ";
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;
52 if ($h > $gal->get('PhotoMaxHeight')) {
53 my $s = $gal->get('PhotoMaxHeight') / $h;
61 $m->{xf} = $f->{xfrm};
68 sub get_meta_exif($$) {
70 $gal->get('CacheExif') or return;
72 my $e = new Image::EXIF($f->{orig});
73 my $i = $e->get_all_info();
75 print "[EXIF error: ", $e->error, "] ";
78 # use Data::Dumper; print Dumper($i);
80 my $lat = $i->{image}->{'Latitude'};
82 if ($lat =~ m{^([NS]) (\d+)\xb0 ([0-9.]+)'$}) {
84 $lat = -$lat if $1 eq 'S';
85 $lat = sprintf "%.6f", $lat;
87 print "[EXIF: unable to parse latitude $lat] ";
92 my $lon = $i->{image}->{'Longitude'};
94 if ($lon =~ m{^([WE]) (\d+)\xb0 ([0-9.]+)'$}) {
96 $lon = -$lon if $1 eq 'W';
97 $lon = sprintf "%.6f", $lon;
99 print "[EXIF: unable to parse longitude $lon] ";
104 my $alt = $i->{image}->{'Altitude'};
106 if ($alt =~ m{^([0-9.]+) m$}) {
109 print "[EXIF: unable to parse altitude $alt] ";
114 # printf "[GEO: lat=%s lon=%s alt=%s] ", $lat // '?', $lon // '?', $alt // '?';
119 $m->{alt} = $alt if $alt;
121 my $time = $i->{image}->{'Image Created'};
123 if ($time =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}:\d{2})$}) {
124 $m->{t} = "$1-$2-$3 $4";
125 # print "[TIME: ", $m->{t}, "] ";
127 print "[EXIF: unable to parse time $time] ";
132 sub generate_photo($$$) {
133 my ($f, $m, $p) = @_;
136 $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
138 $p->SetAttribute(quality=>90);
146 print "-> equalize ";
150 print "-> normalize ";
154 my $rotate = $m->{o};
156 if ($rotate eq "l") { $rot = 270; }
157 elsif ($rotate eq "r") { $rot = 90; }
158 elsif ($rotate eq "u") { $rot = 180; }
160 print "-> rotate $rot ";
161 $p->Rotate(degrees=>$rot);
164 my ($w, $h) = ($m->{w}, $m->{h});
165 if ($w != $m->{w0} || $h != $m->{h0}) {
166 print "-> ${w}x${h} ";
167 $p->Resize(width=>$w, height=>$h);
170 my $photo = $gal->photo_name($m, $f->{id});
171 my $tmp = "$photo.new";
172 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
173 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
176 for my $f (@$orig_list) {
181 $meta->{photo}->{$id} = $m;
182 $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
184 my $p = new Image::Magick;
185 get_meta_basic($f, $m, $p);
186 get_meta_exif($f, $m);
188 my $om = $old_meta->{photo}->{$id};
190 $om->{o} eq $m->{o} &&
191 $om->{xf} eq $m->{xf} &&
192 $om->{w} eq $m->{w} &&
193 $om->{h} eq $m->{h}) {
194 print "... uptodate\n";
198 generate_photo($f, $m, $p);
202 print "Cleaning up stale files\n";
203 for my $f (<$photo_dir/*.jpg>) {
204 my ($vv, $dd, $id) = File::Spec->splitpath($f);
206 unless (defined $meta->{photo}->{$id}) {
207 print "$id: removing\n";
212 print "Writing meta-data\n";
213 $gal->write_meta($photo_meta, $meta);