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