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_dir = $CF{'OrigDir'};
22 my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read gallery.list: $!\n";
24 my $photo_dir = $CF{'PhotoDir'};
26 print "Using existing output directory: $photo_dir\n";
28 print "Creating output directory: $photo_dir\n";
29 File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
32 my $photo_meta = $photo_dir . "/gallery.meta";
35 print "Reading old meta-data\n";
36 $old_meta = UCW::Gallery::ReadMeta($photo_meta);
37 # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
39 my $meta = { 'photo' => {}, 'sequence' => [] };
41 for my $f (@$orig_list) {
43 my $rotate = $f->{orientation};
44 my $xfrm = $f->{xfrm};
47 my $p = new Image::Magick;
48 my $orig = File::Spec->rel2abs($f->{file}, $CF{'OrigDir'});
49 my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($orig) or die "Error reading $orig\n";
50 print "${orig_w}x${orig_h} ";
52 my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
53 my ($w, $h) = ($w0, $h0);
54 if ($w > $CF{'PhotoMaxWidth'}) {
55 my $s = $CF{'PhotoMaxWidth'} / $w;
59 if ($h > $CF{'PhotoMaxHeight'}) {
60 my $s = $CF{'PhotoMaxHeight'} / $h;
72 'title' => $f->{title},
74 $meta->{photo}->{$id} = $m;
75 push @{$meta->{sequence}}, $id;
77 my $om = $old_meta->{photo}->{$id};
79 $om->{o} eq $m->{o} &&
80 $om->{xf} eq $m->{xf} &&
81 $om->{w} eq $m->{w} &&
82 $om->{h} eq $m->{h}) {
83 # FIXME: Remove old images?
84 print "... uptodate\n";
89 $e = $p->Read($orig) and die "Error reading $orig: $e";
91 $p->SetAttribute(quality=>90);
102 print "-> normalize ";
107 if ($rotate eq "l") { $rot = 270; }
108 elsif ($rotate eq "r") { $rot = 90; }
109 elsif ($rotate eq "u") { $rot = 180; }
111 print "-> rotate $rot ";
112 $p->Rotate(degrees=>$rot);
115 if ($w != $w0 || $h != $h0) {
116 print "-> ${w}x${h} ";
117 $p->Resize(width=>$w, height=>$h);
120 my $photo = File::Spec->catfile($photo_dir, $id . ".jpg");
121 my $tmp = "$photo.new";
122 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
123 rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
128 print "Writing meta-data\n";
129 UCW::Gallery::WriteMeta($photo_meta, $meta);