2 # UCW Gallery: Generate published photos
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
10 use UCW::Gallery qw(%CF);
19 UCW::Gallery::LoadConfig;
21 my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read gallery.list: $!\n";
23 my $photo_dir = $CF{'PhotoDir'};
25 print "Using existing output directory: $photo_dir\n";
27 print "Creating output directory: $photo_dir\n";
28 File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
31 my $photo_meta = $photo_dir . "/gallery.meta";
34 print "Reading old meta-data\n";
35 $old_meta = UCW::Gallery::ReadMeta($photo_meta);
36 # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
38 my $meta = { 'photo' => {} };
40 for my $f (@$orig_list) {
42 my $rotate = $f->{orientation};
43 my $xfrm = $f->{xfrm};
46 my $p = new Image::Magick;
47 my $orig = File::Spec->rel2abs($f->{file}, $CF{'OrigDir'});
48 my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($orig) or die "Error reading $orig\n";
49 print "${orig_w}x${orig_h} ";
51 my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
52 my ($w, $h) = ($w0, $h0);
53 if ($w > $CF{'PhotoMaxWidth'}) {
54 my $s = $CF{'PhotoMaxWidth'} / $w;
58 if ($h > $CF{'PhotoMaxHeight'}) {
59 my $s = $CF{'PhotoMaxHeight'} / $h;
72 $meta->{photo}->{$id} = $m;
74 my $om = $old_meta->{photo}->{$id};
76 $om->{o} eq $m->{o} &&
77 $om->{xf} eq $m->{xf} &&
78 $om->{w} eq $m->{w} &&
79 $om->{h} eq $m->{h}) {
80 # FIXME: Remove old images?
81 print "... uptodate\n";
86 $e = $p->Read($orig) and die "Error reading $orig: $e";
88 $p->SetAttribute(quality=>90);
99 print "-> normalize ";
104 if ($rotate eq "l") { $rot = 270; }
105 elsif ($rotate eq "r") { $rot = 90; }
106 elsif ($rotate eq "u") { $rot = 180; }
108 print "-> rotate $rot ";
109 $p->Rotate(degrees=>$rot);
112 if ($w != $w0 || $h != $h0) {
113 print "-> ${w}x${h} ";
114 $p->Resize(width=>$w, height=>$h);
117 my $photo = File::Spec->catfile($photo_dir, $id . ".jpg");
118 my $tmp = "$photo.new";
119 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
120 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
125 print "Writing meta-data\n";
126 UCW::Gallery::WriteMeta($photo_meta, $meta);