2 # UCW Gallery: Prepare cache
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
8 use lib '/home/mj/web/gal2';
18 UCW::Gallery::LoadConfig;
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);
26 my $cache_dir = $UCW::Gallery::CF{'CacheDir'};
28 print "Deleting old cache directory: $cache_dir\n";
29 File::Path::remove_tree($cache_dir);
31 print "Creating cache directory: $cache_dir\n";
32 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
34 for my $t (@{$UCW::Gallery::CF{'ThumbSizes'}}) {
36 my $thumb_fmt = $tw . 'x' . $th;
37 print "Generating $thumb_fmt thumbnails\n";
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";
43 for my $id (@{$meta->{sequence}}) {
44 my $m = $meta->{photo}->{$id} or die;
47 my $p = new Image::Magick;
48 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
50 $e = $p->Read($photo) and die "Error reading $photo: $e";
67 $p->Resize(width=>$w, height=>$h);
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";
74 $thumb_meta->{$id} = {
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);