]> mj.ucw.cz Git - gallery.git/blob - gal/bin/gal-gen
Cleaned up gal-gen
[gallery.git] / gal / bin / gal-gen
1 #!/usr/bin/perl
2 # UCW Gallery: Generate published photos
3 # (c) 2004--2014 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 sub get_meta_basic($$$) {
38         my ($f, $m, $p) = @_;
39         my $rotate = $f->{orientation};
40
41         my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($f->{orig}) or die "Error reading " . $f->{orig} . "\n";
42         print "${orig_w}x${orig_h} ";
43
44         my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
45         my ($w, $h) = ($w0, $h0);
46         if ($w > $gal->get('PhotoMaxWidth')) {
47                 my $s = $gal->get('PhotoMaxWidth') / $w;
48                 $w = $w * $s;
49                 $h = $h * $s;
50         }
51         if ($h > $gal->get('PhotoMaxHeight')) {
52                 my $s = $gal->get('PhotoMaxHeight') / $h;
53                 $w = $w * $s;
54                 $h = $h * $s;
55         }
56         $w = int($w + .5);
57         $h = int($h + .5);
58         
59         $m->{o} = $rotate;
60         $m->{xf} = $f->{xfrm};
61         $m->{w0} = $w0;
62         $m->{h0} = $h0;
63         $m->{w} = $w;
64         $m->{h} = $h;
65 }
66
67 sub generate_photo($$$) {
68         my ($f, $m, $p) = @_;
69
70         my $e;
71         $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e";
72         $p->Strip;
73         $p->SetAttribute(quality=>90);
74
75         my $xfrm = $m->{xf};
76         if ($xfrm =~ /s/) {
77                 print "-> sharpen ";
78                 $p->Sharpen(1);
79         }
80         if ($xfrm =~ /h/) {
81                 print "-> equalize ";
82                 $p->Equalize();
83         }
84         if ($xfrm =~ /n/) {
85                 print "-> normalize ";
86                 $p->Normalize();
87         }
88
89         my $rotate = $m->{o};
90         my $rot = 0;
91         if ($rotate eq "l") { $rot = 270; }
92         elsif ($rotate eq "r") { $rot = 90; }
93         elsif ($rotate eq "u") { $rot = 180; }
94         if ($rot) {
95                 print "-> rotate $rot ";
96                 $p->Rotate(degrees=>$rot);
97         }
98
99         my ($w, $h) = ($m->{w}, $m->{h});
100         if ($w != $m->{w0} || $h != $m->{h0}) {
101                 print "-> ${w}x${h} ";
102                 $p->Resize(width=>$w, height=>$h);
103         }
104
105         my $photo = $gal->photo_name($m, $f->{id});
106         my $tmp = "$photo.new";
107         $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
108         rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
109 }
110
111 for my $f (@$orig_list) {
112         my $id = $f->{id};
113         print "$id: ";
114
115         my $m = { };
116         $meta->{photo}->{$id} = $m;
117         $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
118
119         my $p = new Image::Magick;
120         get_meta_basic($f, $m, $p);
121
122         my $om = $old_meta->{photo}->{$id};
123         if ($om &&
124             $om->{o} eq $m->{o} &&
125             $om->{xf} eq $m->{xf} &&
126             $om->{w} eq $m->{w} &&
127             $om->{h} eq $m->{h}) {
128                 print "... uptodate\n";
129                 next;
130         }
131
132         generate_photo($f, $m, $p);
133         print "... OK\n";
134 }
135
136 print "Cleaning up stale files\n";
137 for my $f (<$photo_dir/*.jpg>) {
138         my ($vv, $dd, $id) = File::Spec->splitpath($f);
139         $id =~ s{\..*$}{};
140         unless (defined $meta->{photo}->{$id}) {
141                 print "$id: removing\n";
142                 unlink $f;
143         }
144 }
145
146 print "Writing meta-data\n";
147 $gal->write_meta($photo_meta, $meta);
148 exit 0;