2 # UCW Gallery: Generate published photos
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
16 my $gal = UCW::Gallery->load_config;
18 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
20 my $photo_dir = $gal->get('PhotoDir');
22 print "Using existing output directory: $photo_dir\n";
24 print "Creating output directory: $photo_dir\n";
25 File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
28 my $photo_meta = $gal->photo_meta_name;
31 print "Reading old meta-data\n";
32 $old_meta = $gal->read_meta($photo_meta);
33 # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
35 my $meta = { 'photo' => {} };
37 sub get_meta_basic($$$) {
39 my $rotate = $f->{orientation};
41 my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($f->{orig}) or die "Error reading " . $f->{orig} . "\n";
42 print "${orig_w}x${orig_h} ";
44 my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
45 my ($w, $h) = ($w0, $h0);
46 if ($w > $gal->get('PhotoMaxWidth')) {
47 my $s = $gal->get('PhotoMaxWidth') / $w;
51 if ($h > $gal->get('PhotoMaxHeight')) {
52 my $s = $gal->get('PhotoMaxHeight') / $h;
60 for my $k (keys %UCW::Gallery::list_attrs) {
61 next if $UCW::Gallery::list_attrs{$k} < 2;
62 $m->{$k} = $f->{$k} if defined $f->{$k};
73 if ($g =~ m{^([NEWS]) (\d+)\xb0 ([0-9.]+)'$}) {
75 $g = -$g if $1 eq 'W' || $1 eq 'S';
76 } elsif ($g =~ m{^([NEWS]) (\d+)\xb0 (\d+)' ([0-9.]+)$}) {
77 $g = $2 + $3/60 + $4/3600;
78 $g = -$g if $1 eq 'W' || $1 eq 'S';
80 print "[EXIF: unable to parse coordinate $g] ";
83 return sprintf "%.6f", $g;
86 sub get_meta_exif($$) {
88 $gal->get('CacheExif') or return;
90 my $e = new Image::EXIF($f->{orig});
91 my $i = $e->get_all_info();
93 print "[EXIF error: ", $e->error, "] ";
96 # use Data::Dumper; print Dumper($i);
98 my $lat = parse_geo($i->{image}->{'Latitude'});
99 my $lon = parse_geo($i->{image}->{'Longitude'});
101 my $alt = $i->{image}->{'Altitude'};
103 if ($alt =~ m{^([0-9.]+) m$}) {
106 print "[EXIF: unable to parse altitude $alt] ";
111 # printf "[GEO: lat=%s lon=%s alt=%s] ", $lat // '?', $lon // '?', $alt // '?';
116 $m->{alt} //= $alt if $alt;
118 my $time = $i->{image}->{'Image Created'};
120 if ($time =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}:\d{2})$}) {
121 $m->{t} //= "$1-$2-$3 $4";
122 # print "[TIME: ", $m->{t}, "] ";
124 print "[EXIF: unable to parse time $time] ";
129 sub generate_photo($$$) {
130 my ($f, $m, $p) = @_;
133 $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
135 $p->SetAttribute(quality=>90);
143 print "-> equalize ";
147 print "-> normalize ";
151 my $rotate = $m->{o};
153 if ($rotate eq "l") { $rot = 270; }
154 elsif ($rotate eq "r") { $rot = 90; }
155 elsif ($rotate eq "u") { $rot = 180; }
157 print "-> rotate $rot ";
158 $p->Rotate(degrees=>$rot);
161 my ($w, $h) = ($m->{w}, $m->{h});
162 if ($w != $m->{w0} || $h != $m->{h0}) {
163 print "-> ${w}x${h} ";
164 $p->Resize(width=>$w, height=>$h);
167 my $photo = $gal->photo_name($m, $f->{id});
168 my $tmp = "$photo.new";
169 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
170 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
173 for my $f (@$orig_list) {
178 $meta->{photo}->{$id} = $m;
179 $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
181 my $p = new Image::Magick;
182 get_meta_basic($f, $m, $p);
183 get_meta_exif($f, $m);
185 my $om = $old_meta->{photo}->{$id};
187 $om->{o} eq $m->{o} &&
188 $om->{xf} eq $m->{xf} &&
189 $om->{w} eq $m->{w} &&
190 $om->{h} eq $m->{h}) {
191 print "... uptodate\n";
195 generate_photo($f, $m, $p);
199 print "Cleaning up stale files\n";
200 for my $f (<$photo_dir/*.jpg>) {
201 my ($vv, $dd, $id) = File::Spec->splitpath($f);
203 unless (defined $meta->{photo}->{$id}) {
204 print "$id: removing\n";
209 print "Writing meta-data\n";
210 $gal->write_meta($photo_meta, $meta);