]> mj.ucw.cz Git - gallery.git/blob - gal/Gallery/Generator.pm
Fix bugs caused by recent changes
[gallery.git] / gal / Gallery / Generator.pm
1 # Simple Photo Gallery: Thumbnail Generator
2 # (c) 2003--2004 Martin Mares <mj@ucw.cz>
3
4 package Gallery::Generator;
5
6 use strict;
7 use warnings;
8
9 use Image::Magick;
10 use IO::Handle;
11
12 BEGIN { import Gallery qw(%CF); }
13
14 sub new() { return bless {}; }
15
16 sub Start($) {
17         print "Generating thumbnails...\n"; STDOUT->autoflush(1);
18         my $notes = $CF{'MetaDataDir'} . "/notes";
19         open NOTES, ">$notes" or die "Unable to write to notes ($notes): $!";
20 }
21
22 sub Finish($) {
23         close NOTES;
24         print "Done.\n";
25 }
26
27 sub RawHTML($$) { }
28
29 sub img($$$) {
30         my ($this, $orig, $annot) = @_;
31         my ($base, $ext) = ($orig =~ /^(.*)\.([^.]+)$/) or die "Unable to dissect name $orig";
32         print "$base: ";
33         my $p = new Image::Magick;
34         my $e;
35         my $img = $CF{"PhotoDir"} . "/$orig";
36         $e = $p->Read($img) and die "Error reading $img: $e";
37         my ($wo, $ho) = $p->Get('width', 'height');
38         my ($w, $h) = ($wo, $ho);
39         print "${w}x${h}";
40         $p->Strip;
41         if ($w > $CF{"ThumbW"}) {
42                 my $s = $CF{"ThumbW"} / $w;
43                 $w = $w * $s;
44                 $h = $h * $s;
45         }
46         if ($h > $CF{"ThumbH"}) {
47                 my $s = $CF{"ThumbH"} / $h;
48                 $w = $w * $s;
49                 $h = $h * $s;
50         }
51         $w = int($w);
52         $h = int($h);
53         print " -> ${w}x${h} ";
54         $p->Resize(width=>$w, height=>$h);
55         my $thumb = $CF{"ThumbDir"} . "/$base-thumb.jpg";
56         $e = $p->Write($thumb) and die "Unable to write $thumb: $e";
57         print NOTES "$base $w $h $wo $ho\n";
58         print "OK\n";
59 }
60
61 1;