]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-cache
Gallery2: More objectification
[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;
11
12 use Image::Magick;
13 use IO::Handle;
14 use File::Spec;
15 use File::Path;
16
17 STDOUT->autoflush(1);
18
19 my $gal = UCW::Gallery->load_config;
20
21 print "Reading gallery.list\n";
22 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
23
24 my $photo_dir = $gal->get('PhotoDir');
25 my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
26 print "Reading meta-data from $photo_meta\n";
27 -f $photo_meta or die "Cannot load $photo_meta\n";
28 my $meta = $gal->read_meta($photo_meta);
29
30 my $cache_dir = $gal->get('CacheDir');
31 if (-d $cache_dir) {
32         print "Deleting old cache directory: $cache_dir\n";
33         File::Path::remove_tree($cache_dir);
34 }
35 print "Creating cache directory: $cache_dir\n";
36 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
37
38 # Construct sequence and store photo titles
39 $meta->{sequence} = [];
40 for my $f (@$orig_list) {
41         push @{$meta->{sequence}}, $f->{id};
42         my $m = $meta->{photo}->{$f->{id}} or die;
43         $m->{title} = $f->{title};
44 }
45
46 for my $thumb_fmt (keys %{$gal->get('ThumbFormats')}) {
47         my ($tw, $th) = ($thumb_fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $thumb_fmt\n";
48         print "Generating $thumb_fmt thumbnails\n";
49         my $thumb_meta = {};
50         $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
51         my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
52         -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
53
54         for my $id (@{$meta->{sequence}}) {
55                 my $m = $meta->{photo}->{$id} or die;
56                 print "\t$id: ";
57
58                 my $p = new Image::Magick;
59                 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
60                 my $e;
61                 $e = $p->Read($photo) and die "Error reading $photo: $e";
62
63                 my $w = $m->{w};
64                 my $h = $m->{h};
65                 if ($w > $tw) {
66                         my $s = $tw / $w;
67                         $w = $w * $s;
68                         $h = $h * $s;
69                 }
70                 if ($h > $th) {
71                         my $s = $th / $h;
72                         $w = $w * $s;
73                         $h = $h * $s;
74                 }
75                 $w = int($w);
76                 $h = int($h);
77                 print "${w}x${h} ";
78                 $p->Resize(width=>$w, height=>$h);
79
80                 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
81                 my $tmp = "$photo.new";
82                 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
83                 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
84
85                 $thumb_meta->{$id} = {
86                         'w' => $w,
87                         'h' => $h,
88                 };
89
90                 print "... OK\n";
91         }
92 }
93
94 my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
95 print "Writing meta-data to $cache_meta\n";
96 $gal->write_meta($cache_meta, $meta);