]> mj.ucw.cz Git - gallery.git/commitdiff
Gallery2: gen-cache
authorMartin Mares <mj@ucw.cz>
Sun, 23 Dec 2012 22:47:17 +0000 (23:47 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:08 +0000 (21:14 +0100)
gal2/FORMAT
gal2/UCW/Gallery.pm
gal2/gal-cache [new file with mode: 0755]
gal2/gal-gen

index b7a67fd0526e79bf840ffb0a68a51a3e645ac311..2f4a1ba5da7a169eda9d309b4e7a58cc5de58433 100644 (file)
@@ -14,8 +14,8 @@ One photo per line, tab-separated columns:
 Lines starting with "#" are ignored.
 
 
-Photo meta-data (gallery.meta in PhotoDir)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Photo meta-data (PhotoDir/gallery.meta or CacheDir/cache.meta)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Perl Storable containing a single hash.
 
 $meta->{photo}->{$identifier} is a hash of:
@@ -23,3 +23,11 @@ $meta->{photo}->{$identifier} is a hash of:
        xf              transformation applied
        w               width after scaling
        h               height after scaling
+       title           photo title
+
+$meta->{sequence} is an array of photo IDs as they appear in the gallery.
+
+[CacheDir only]
+$meta->{thumb}->{$format}->{$identifier} is a hash of:
+       w               thumbnail width
+       h               thumbnail height
index f13889d2d032f936dde83e04ac89ca51a8a54b1f..847de01987b3b1f226424ed13e483743111ff5cf 100644 (file)
@@ -37,12 +37,14 @@ BEGIN {
        $CF{'MetaDataDir'} = '.',
        $CF{'PhotoDir'} = '.',
 
-       $CF{'ScanDefaultTransform'} = 'n';
+       $CF{'ScanDefaultTransform'} = 's';
        $CF{'OrigDir'} = '.';
        $CF{'PhotoDir'} = 'photo';
-       $CF{'CacheDir'} = "cache",
+       $CF{'CacheDir'} = 'cache',
        $CF{'PhotoMaxWidth'} = 1024,
        $CF{'PhotoMaxHeight'} = 1024,
+       # FIXME: ThumbSizes should be set by themes
+       $CF{'ThumbSizes'} = [ [114,94], [256,256] ],
 }
 
 sub LoadConfig() {
diff --git a/gal2/gal-cache b/gal2/gal-cache
new file mode 100755 (executable)
index 0000000..426335e
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/perl
+# UCW Gallery: Prepare cache
+# (c) 2004--2012 Martin Mares <mj@ucw.cz>
+
+use strict;
+use warnings;
+
+use lib '/home/mj/web/gal2';
+use UCW::Gallery;
+
+use Image::Magick;
+use IO::Handle;
+use File::Spec;
+use File::Path;
+
+STDOUT->autoflush(1);
+
+UCW::Gallery::LoadConfig;
+
+my $photo_dir = $UCW::Gallery::CF{'PhotoDir'};
+my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
+print "Reading meta-data from $photo_meta\n";
+-f $photo_meta or die "Cannot load $photo_meta\n";
+my $meta = UCW::Gallery::ReadMeta($photo_meta);
+
+my $cache_dir = $UCW::Gallery::CF{'CacheDir'};
+if (-d $cache_dir) {
+       print "Deleting old cache directory: $cache_dir\n";
+       File::Path::remove_tree($cache_dir);
+}
+print "Creating cache directory: $cache_dir\n";
+File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
+
+for my $t (@{$UCW::Gallery::CF{'ThumbSizes'}}) {
+       my ($tw, $th) = @$t;
+       my $thumb_fmt = $tw . 'x' . $th;
+       print "Generating $thumb_fmt thumbnails\n";
+       my $thumb_meta = {};
+       $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
+       my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
+       -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
+
+       for my $id (@{$meta->{sequence}}) {
+               my $m = $meta->{photo}->{$id} or die;
+               print "\t$id: ";
+
+               my $p = new Image::Magick;
+               my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
+               my $e;
+               $e = $p->Read($photo) and die "Error reading $photo: $e";
+
+               my $w = $m->{w};
+               my $h = $m->{h};
+               if ($w > $tw) {
+                       my $s = $tw / $w;
+                       $w = $w * $s;
+                       $h = $h * $s;
+               }
+               if ($h > $th) {
+                       my $s = $th / $h;
+                       $w = $w * $s;
+                       $h = $h * $s;
+               }
+               $w = int($w);
+               $h = int($h);
+               print "${w}x${h} ";
+               $p->Resize(width=>$w, height=>$h);
+
+               my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
+               my $tmp = "$photo.new";
+               $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
+               rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
+
+               $thumb_meta->{$id} = {
+                       'w' => $w,
+                       'h' => $h,
+               };
+
+               print "... OK\n";
+       }
+}
+
+my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
+print "Writing meta-data to $cache_meta\n";
+UCW::Gallery::WriteMeta($cache_meta, $meta);
index 970e3d2b9950aeb11e9ffe2d9fb85b6c142664f1..d8edb57e6ba7e00401ebf1595136a2e9339a08d3 100755 (executable)
@@ -8,7 +8,6 @@ use warnings;
 use lib '/home/mj/web/gal2';
 use UCW::Gallery;
 
-use File::Spec;
 use Image::Magick;
 use IO::Handle;
 use File::Spec;
@@ -23,9 +22,9 @@ my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read galle
 
 my $photo_dir = $UCW::Gallery::CF{'PhotoDir'};
 if (-d $photo_dir) {
-       print "Using existing output directory $photo_dir\n";
+       print "Using existing output directory: $photo_dir\n";
 } else {
-       print "Creating output directory $photo_dir\n";
+       print "Creating output directory: $photo_dir\n";
        File::Path::mkpath($photo_dir) or die "Unable to create $photo_dir: $!\n";
 }
 
@@ -36,12 +35,13 @@ if (-f $photo_meta) {
        $old_meta = UCW::Gallery::ReadMeta($photo_meta);
        # use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
 }
-my $meta = { 'photo' => {} };
+my $meta = { 'photo' => {}, 'sequence' => [] };
 
 for my $f (@$orig_list) {
        my $id = $f->{id};
        my $rotate = $f->{orientation};
        my $xfrm = $f->{xfrm};
+       push @{$meta->{sequence}}, $id;
        print "$id: ";
 
        my $p = new Image::Magick;