]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-cache
Gallery2: gen-cache
[gallery.git] / gal2 / gal-cache
1 #!/usr/bin/perl
2 # UCW Gallery: Prepare cache
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;
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 $photo_dir = $UCW::Gallery::CF{'PhotoDir'};
21 my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
22 print "Reading meta-data from $photo_meta\n";
23 -f $photo_meta or die "Cannot load $photo_meta\n";
24 my $meta = UCW::Gallery::ReadMeta($photo_meta);
25
26 my $cache_dir = $UCW::Gallery::CF{'CacheDir'};
27 if (-d $cache_dir) {
28         print "Deleting old cache directory: $cache_dir\n";
29         File::Path::remove_tree($cache_dir);
30 }
31 print "Creating cache directory: $cache_dir\n";
32 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
33
34 for my $t (@{$UCW::Gallery::CF{'ThumbSizes'}}) {
35         my ($tw, $th) = @$t;
36         my $thumb_fmt = $tw . 'x' . $th;
37         print "Generating $thumb_fmt thumbnails\n";
38         my $thumb_meta = {};
39         $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
40         my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
41         -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
42
43         for my $id (@{$meta->{sequence}}) {
44                 my $m = $meta->{photo}->{$id} or die;
45                 print "\t$id: ";
46
47                 my $p = new Image::Magick;
48                 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
49                 my $e;
50                 $e = $p->Read($photo) and die "Error reading $photo: $e";
51
52                 my $w = $m->{w};
53                 my $h = $m->{h};
54                 if ($w > $tw) {
55                         my $s = $tw / $w;
56                         $w = $w * $s;
57                         $h = $h * $s;
58                 }
59                 if ($h > $th) {
60                         my $s = $th / $h;
61                         $w = $w * $s;
62                         $h = $h * $s;
63                 }
64                 $w = int($w);
65                 $h = int($h);
66                 print "${w}x${h} ";
67                 $p->Resize(width=>$w, height=>$h);
68
69                 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
70                 my $tmp = "$photo.new";
71                 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
72                 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
73
74                 $thumb_meta->{$id} = {
75                         'w' => $w,
76                         'h' => $h,
77                 };
78
79                 print "... OK\n";
80         }
81 }
82
83 my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
84 print "Writing meta-data to $cache_meta\n";
85 UCW::Gallery::WriteMeta($cache_meta, $meta);