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