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