]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-cache
591a1cee3082e86d1778a06dbf43f63bd70ca8fb
[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 FindBin;
9 use lib $FindBin::Bin;
10 use UCW::Gallery qw(%CF);
11
12 use Image::Magick;
13 use IO::Handle;
14 use File::Spec;
15 use File::Path;
16
17 STDOUT->autoflush(1);
18
19 UCW::Gallery::LoadConfig;
20
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);
26
27 my $cache_dir = $CF{'CacheDir'};
28 if (-d $cache_dir) {
29         print "Deleting old cache directory: $cache_dir\n";
30         File::Path::remove_tree($cache_dir);
31 }
32 print "Creating cache directory: $cache_dir\n";
33 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
34
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";
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);