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};
71 if ($g =~ m{^([NEWS]) (\d+)\xb0 ([0-9.]+)'$}) {
73 $g = -$g if $1 eq 'W' || $1 eq 'S';
74 } elsif ($g =~ m{^([NEWS]) (\d+)\xb0 (\d+)' ([0-9.]+)$}) {
75 $g = $2 + $3/60 + $4/3600;
76 $g = -$g if $1 eq 'W' || $1 eq 'S';
78 print "[EXIF: unable to parse coordinate $g] ";
81 return sprintf "%.6f", $g;
84 sub get_meta_exif($$) {
86 $gal->get('CacheExif') or return;
88 my $e = new Image::EXIF($f->{orig});
89 my $i = $e->get_all_info();
91 print "[EXIF error: ", $e->error, "] ";
94 # use Data::Dumper; print Dumper($i);
96 my $lat = parse_geo($i->{image}->{'Latitude'});
97 my $lon = parse_geo($i->{image}->{'Longitude'});
99 my $alt = $i->{image}->{'Altitude'};
101 if ($alt =~ m{^([0-9.]+) m$}) {
104 print "[EXIF: unable to parse altitude $alt] ";
109 # printf "[GEO: lat=%s lon=%s alt=%s] ", $lat // '?', $lon // '?', $alt // '?';
114 $m->{alt} = $alt if $alt;
116 my $time = $i->{image}->{'Image Created'};
118 if ($time =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}:\d{2})$}) {
119 $m->{t} = "$1-$2-$3 $4";
120 # print "[TIME: ", $m->{t}, "] ";
122 print "[EXIF: unable to parse time $time] ";
127 sub generate_photo($$$) {
128 my ($f, $m, $p) = @_;
131 $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
133 $p->SetAttribute(quality=>90);
141 print "-> equalize ";
145 print "-> normalize ";
149 my $rotate = $m->{o};
151 if ($rotate eq "l") { $rot = 270; }
152 elsif ($rotate eq "r") { $rot = 90; }
153 elsif ($rotate eq "u") { $rot = 180; }
155 print "-> rotate $rot ";
156 $p->Rotate(degrees=>$rot);
159 my ($w, $h) = ($m->{w}, $m->{h});
160 if ($w != $m->{w0} || $h != $m->{h0}) {
161 print "-> ${w}x${h} ";
162 $p->Resize(width=>$w, height=>$h);
165 my $photo = $gal->photo_name($m, $f->{id});
166 my $tmp = "$photo.new";
167 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
168 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
171 for my $f (@$orig_list) {
176 $meta->{photo}->{$id} = $m;
177 $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
179 my $p = new Image::Magick;
180 get_meta_basic($f, $m, $p);
181 get_meta_exif($f, $m);
183 my $om = $old_meta->{photo}->{$id};
185 $om->{o} eq $m->{o} &&
186 $om->{xf} eq $m->{xf} &&
187 $om->{w} eq $m->{w} &&
188 $om->{h} eq $m->{h}) {
189 print "... uptodate\n";
193 generate_photo($f, $m, $p);
197 print "Cleaning up stale files\n";
198 for my $f (<$photo_dir/*.jpg>) {
199 my ($vv, $dd, $id) = File::Spec->splitpath($f);
201 unless (defined $meta->{photo}->{$id}) {
202 print "$id: removing\n";
207 print "Writing meta-data\n";
208 $gal->write_meta($photo_meta, $meta);