2 # UCW Gallery: Generate published photos
3 # (c) 2004--2012 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 for my $f (@$orig_list) {
39 my $rotate = $f->{orientation};
40 my $xfrm = $f->{xfrm};
43 my $p = new Image::Magick;
44 my $orig = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
45 my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($orig) or die "Error reading $orig\n";
46 print "${orig_w}x${orig_h} ";
48 my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
49 my ($w, $h) = ($w0, $h0);
50 if ($w > $gal->get('PhotoMaxWidth')) {
51 my $s = $gal->get('PhotoMaxWidth') / $w;
55 if ($h > $gal->get('PhotoMaxHeight')) {
56 my $s = $gal->get('PhotoMaxHeight') / $h;
69 $meta->{photo}->{$id} = $m;
71 my $om = $old_meta->{photo}->{$id};
73 $om->{o} eq $m->{o} &&
74 $om->{xf} eq $m->{xf} &&
75 $om->{w} eq $m->{w} &&
76 $om->{h} eq $m->{h}) {
77 print "... uptodate\n";
82 $e = $p->Read($orig) and die "Error reading $orig: $e";
84 $p->SetAttribute(quality=>90);
95 print "-> normalize ";
100 if ($rotate eq "l") { $rot = 270; }
101 elsif ($rotate eq "r") { $rot = 90; }
102 elsif ($rotate eq "u") { $rot = 180; }
104 print "-> rotate $rot ";
105 $p->Rotate(degrees=>$rot);
108 if ($w != $w0 || $h != $h0) {
109 print "-> ${w}x${h} ";
110 $p->Resize(width=>$w, height=>$h);
113 my $photo = File::Spec->catfile($photo_dir, $gal->photo_meta($m, $id));
114 my $tmp = "$photo.new";
115 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
116 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
121 print "Cleaning up stale files\n";
122 for my $f (<$photo_dir/*.jpg>) {
123 my ($vv, $dd, $id) = File::Spec->splitpath($f);
125 unless (defined $meta->{photo}->{$id}) {
126 print "$id: removing\n";
131 print "Writing meta-data\n";
132 $gal->write_meta($photo_meta, $meta);