]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-gen
1e7c3dd6d1562038f93ebf8ddbafdb5102b60b76
[gallery.git] / gal2 / gal-gen
1 #!/usr/bin/perl
2 # UCW Gallery: Generate published photos
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 use FindBin;
9 use lib $FindBin::Bin;
10 use UCW::Gallery qw(%CF);
11
12 use Image::Magick;
13 use IO::Handle;
14 use File::Spec;
15 use File::Path;
16
17 STDOUT->autoflush(1);
18
19 UCW::Gallery::LoadConfig;
20
21 my $orig_dir = $CF{'OrigDir'};
22 my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read gallery.list: $!\n";
23
24 my $photo_dir = $CF{'PhotoDir'};
25 if (-d $photo_dir) {
26         print "Using existing output directory: $photo_dir\n";
27 } else {
28         print "Creating output directory: $photo_dir\n";
29         File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
30 }
31
32 my $photo_meta = $photo_dir . "/gallery.meta";
33 my $old_meta = {};
34 if (-f $photo_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";
38 }
39 my $meta = { 'photo' => {}, 'sequence' => [] };
40
41 for my $f (@$orig_list) {
42         my $id = $f->{id};
43         my $rotate = $f->{orientation};
44         my $xfrm = $f->{xfrm};
45         print "$id: ";
46
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} ";
51
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;
56                 $w = $w * $s;
57                 $h = $h * $s;
58         }
59         if ($h > $CF{'PhotoMaxHeight'}) {
60                 my $s = $CF{'PhotoMaxHeight'} / $h;
61                 $w = $w * $s;
62                 $h = $h * $s;
63         }
64         $w = int($w);
65         $h = int($h);
66         
67         my $m = {
68                 'o' => $rotate,
69                 'xf' => $xfrm,
70                 'w' => $w,
71                 'h' => $h,
72                 'title' => $f->{title},
73         };
74         $meta->{photo}->{$id} = $m;
75         push @{$meta->{sequence}}, $id;
76
77         my $om = $old_meta->{photo}->{$id};
78         if ($om &&
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";
85                 next;
86         }
87
88         my $e;
89         $e = $p->Read($orig) and die "Error reading $orig: $e";
90         $p->Strip;
91         $p->SetAttribute(quality=>90);
92
93         if ($xfrm =~ /s/) {
94                 print "-> sharpen ";
95                 $p->Sharpen(1);
96         }
97         if ($xfrm =~ /h/) {
98                 print "-> equalize ";
99                 $p->Equalize();
100         }
101         if ($xfrm =~ /n/) {
102                 print "-> normalize ";
103                 $p->Normalize();
104         }
105
106         my $rot = 0;
107         if ($rotate eq "l") { $rot = 270; }
108         elsif ($rotate eq "r") { $rot = 90; }
109         elsif ($rotate eq "u") { $rot = 180; }
110         if ($rot) {
111                 print "-> rotate $rot ";
112                 $p->Rotate(degrees=>$rot);
113         }
114
115         if ($w != $w0 || $h != $h0) {
116                 print "-> ${w}x${h} ";
117                 $p->Resize(width=>$w, height=>$h);
118         }
119
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";
124
125         print "... OK\n";
126 }
127
128 print "Writing meta-data\n";
129 UCW::Gallery::WriteMeta($photo_meta, $meta);