2 # UCW Gallery: Prepare cache
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
10 use UCW::Gallery qw(%CF);
19 UCW::Gallery::LoadConfig;
21 my $photo_dir = $CF{'PhotoDir'};
22 my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
23 print "Reading meta-data from $photo_meta\n";
24 -f $photo_meta or die "Cannot load $photo_meta\n";
25 my $meta = UCW::Gallery::ReadMeta($photo_meta);
27 my $cache_dir = $CF{'CacheDir'};
29 print "Deleting old cache directory: $cache_dir\n";
30 File::Path::remove_tree($cache_dir);
32 print "Creating cache directory: $cache_dir\n";
33 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
35 for my $thumb_fmt (keys %{$CF{'ThumbFormats'}}) {
36 my ($tw, $th) = ($thumb_fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $thumb_fmt\n";
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);