]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-cache
Gallery2: Missing bits
[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 qw(%CF);
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 = $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 = $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 $thumb_fmt (keys %{$CF{'ThumbFormats'}}) {
35         my ($tw, $th) = ($thumb_fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $thumb_fmt\n";
36         print "Generating $thumb_fmt thumbnails\n";
37         my $thumb_meta = {};
38         $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
39         my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
40         -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
41
42         for my $id (@{$meta->{sequence}}) {
43                 my $m = $meta->{photo}->{$id} or die;
44                 print "\t$id: ";
45
46                 my $p = new Image::Magick;
47                 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
48                 my $e;
49                 $e = $p->Read($photo) and die "Error reading $photo: $e";
50
51                 my $w = $m->{w};
52                 my $h = $m->{h};
53                 if ($w > $tw) {
54                         my $s = $tw / $w;
55                         $w = $w * $s;
56                         $h = $h * $s;
57                 }
58                 if ($h > $th) {
59                         my $s = $th / $h;
60                         $w = $w * $s;
61                         $h = $h * $s;
62                 }
63                 $w = int($w);
64                 $h = int($h);
65                 print "${w}x${h} ";
66                 $p->Resize(width=>$w, height=>$h);
67
68                 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
69                 my $tmp = "$photo.new";
70                 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
71                 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
72
73                 $thumb_meta->{$id} = {
74                         'w' => $w,
75                         'h' => $h,
76                 };
77
78                 print "... OK\n";
79         }
80 }
81
82 my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
83 print "Writing meta-data to $cache_meta\n";
84 UCW::Gallery::WriteMeta($cache_meta, $meta);