]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-gen
Gallery2: Delete old theme "nrt"
[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;
11
12 use Image::Magick;
13 use IO::Handle;
14 use File::Spec;
15 use File::Path;
16
17 STDOUT->autoflush(1);
18
19 my $gal = UCW::Gallery->load_config;
20
21 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
22
23 my $photo_dir = $gal->get('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 = $gal->read_meta($photo_meta);
36         # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
37 }
38 my $meta = { 'photo' => {} };
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}, $gal->get('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 > $gal->get('PhotoMaxWidth')) {
54                 my $s = $gal->get('PhotoMaxWidth') / $w;
55                 $w = $w * $s;
56                 $h = $h * $s;
57         }
58         if ($h > $gal->get('PhotoMaxHeight')) {
59                 my $s = $gal->get('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         };
72         $meta->{photo}->{$id} = $m;
73
74         my $om = $old_meta->{photo}->{$id};
75         if ($om &&
76             $om->{o} eq $m->{o} &&
77             $om->{xf} eq $m->{xf} &&
78             $om->{w} eq $m->{w} &&
79             $om->{h} eq $m->{h}) {
80                 # FIXME: Remove old images?
81                 print "... uptodate\n";
82                 next;
83         }
84
85         my $e;
86         $e = $p->Read($orig) and die "Error reading $orig: $e";
87         $p->Strip;
88         $p->SetAttribute(quality=>90);
89
90         if ($xfrm =~ /s/) {
91                 print "-> sharpen ";
92                 $p->Sharpen(1);
93         }
94         if ($xfrm =~ /h/) {
95                 print "-> equalize ";
96                 $p->Equalize();
97         }
98         if ($xfrm =~ /n/) {
99                 print "-> normalize ";
100                 $p->Normalize();
101         }
102
103         my $rot = 0;
104         if ($rotate eq "l") { $rot = 270; }
105         elsif ($rotate eq "r") { $rot = 90; }
106         elsif ($rotate eq "u") { $rot = 180; }
107         if ($rot) {
108                 print "-> rotate $rot ";
109                 $p->Rotate(degrees=>$rot);
110         }
111
112         if ($w != $w0 || $h != $h0) {
113                 print "-> ${w}x${h} ";
114                 $p->Resize(width=>$w, height=>$h);
115         }
116
117         my $photo = File::Spec->catfile($photo_dir, $id . ".jpg");
118         my $tmp = "$photo.new";
119         $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
120         rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n";
121
122         print "... OK\n";
123 }
124
125 print "Writing meta-data\n";
126 $gal->write_meta($photo_meta, $meta);