]> mj.ucw.cz Git - gallery.git/blob - gal/bin/gal-gen
UCW::Gallery::Web::Highslide: Image pages do not include JS
[gallery.git] / gal / bin / 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 UCW::Gallery;
9 use Image::Magick;
10 use IO::Handle;
11 use File::Spec;
12 use File::Path;
13
14 STDOUT->autoflush(1);
15
16 my $gal = UCW::Gallery->load_config;
17
18 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
19
20 my $photo_dir = $gal->get('PhotoDir');
21 if (-d $photo_dir) {
22         print "Using existing output directory: $photo_dir\n";
23 } else {
24         print "Creating output directory: $photo_dir\n";
25         File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
26 }
27
28 my $photo_meta = $gal->photo_meta_name;
29 my $old_meta = {};
30 if (-f $photo_meta) {
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";
34 }
35 my $meta = { 'photo' => {} };
36
37 for my $f (@$orig_list) {
38         my $id = $f->{id};
39         my $rotate = $f->{orientation};
40         my $xfrm = $f->{xfrm};
41         print "$id: ";
42
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} ";
47
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;
52                 $w = $w * $s;
53                 $h = $h * $s;
54         }
55         if ($h > $gal->get('PhotoMaxHeight')) {
56                 my $s = $gal->get('PhotoMaxHeight') / $h;
57                 $w = $w * $s;
58                 $h = $h * $s;
59         }
60         $w = int($w + .5);
61         $h = int($h + .5);
62         
63         my $m = {
64                 'o' => $rotate,
65                 'xf' => $xfrm,
66                 'w' => $w,
67                 'h' => $h,
68         };
69         $meta->{photo}->{$id} = $m;
70
71         my $om = $old_meta->{photo}->{$id};
72         if ($om &&
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";
78                 next;
79         }
80
81         my $e;
82         $e = $p->Read($orig) and die "Error reading $orig: $e";
83         $p->Strip;
84         $p->SetAttribute(quality=>90);
85
86         if ($xfrm =~ /s/) {
87                 print "-> sharpen ";
88                 $p->Sharpen(1);
89         }
90         if ($xfrm =~ /h/) {
91                 print "-> equalize ";
92                 $p->Equalize();
93         }
94         if ($xfrm =~ /n/) {
95                 print "-> normalize ";
96                 $p->Normalize();
97         }
98
99         my $rot = 0;
100         if ($rotate eq "l") { $rot = 270; }
101         elsif ($rotate eq "r") { $rot = 90; }
102         elsif ($rotate eq "u") { $rot = 180; }
103         if ($rot) {
104                 print "-> rotate $rot ";
105                 $p->Rotate(degrees=>$rot);
106         }
107
108         if ($w != $w0 || $h != $h0) {
109                 print "-> ${w}x${h} ";
110                 $p->Resize(width=>$w, height=>$h);
111         }
112
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";
117
118         print "... OK\n";
119 }
120
121 print "Cleaning up stale files\n";
122 for my $f (<$photo_dir/*.jpg>) {
123         my ($vv, $dd, $id) = File::Spec->splitpath($f);
124         $id =~ s{\..*$}{};
125         unless (defined $meta->{photo}->{$id}) {
126                 print "$id: removing\n";
127                 unlink $f;
128         }
129 }
130
131 print "Writing meta-data\n";
132 $gal->write_meta($photo_meta, $meta);