2 # UCW Gallery: Generate published photos
3 # (c) 2004--2014 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 $m->{xf} = $f->{xfrm};
67 sub generate_photo($$$) {
71 $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
73 $p->SetAttribute(quality=>90);
85 print "-> normalize ";
91 if ($rotate eq "l") { $rot = 270; }
92 elsif ($rotate eq "r") { $rot = 90; }
93 elsif ($rotate eq "u") { $rot = 180; }
95 print "-> rotate $rot ";
96 $p->Rotate(degrees=>$rot);
99 my ($w, $h) = ($m->{w}, $m->{h});
100 if ($w != $m->{w0} || $h != $m->{h0}) {
101 print "-> ${w}x${h} ";
102 $p->Resize(width=>$w, height=>$h);
105 my $photo = $gal->photo_name($m, $f->{id});
106 my $tmp = "$photo.new";
107 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
108 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
111 for my $f (@$orig_list) {
116 $meta->{photo}->{$id} = $m;
117 $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
119 my $p = new Image::Magick;
120 get_meta_basic($f, $m, $p);
122 my $om = $old_meta->{photo}->{$id};
124 $om->{o} eq $m->{o} &&
125 $om->{xf} eq $m->{xf} &&
126 $om->{w} eq $m->{w} &&
127 $om->{h} eq $m->{h}) {
128 print "... uptodate\n";
132 generate_photo($f, $m, $p);
136 print "Cleaning up stale files\n";
137 for my $f (<$photo_dir/*.jpg>) {
138 my ($vv, $dd, $id) = File::Spec->splitpath($f);
140 unless (defined $meta->{photo}->{$id}) {
141 print "$id: removing\n";
146 print "Writing meta-data\n";
147 $gal->write_meta($photo_meta, $meta);