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