From: Martin Mares Date: Sun, 23 Dec 2012 22:47:17 +0000 (+0100) Subject: Gallery2: gen-cache X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=b89f977dc5a5d785766118992aa90a7eff7b5d3b;p=gallery.git Gallery2: gen-cache --- diff --git a/gal2/FORMAT b/gal2/FORMAT index b7a67fd..2f4a1ba 100644 --- a/gal2/FORMAT +++ b/gal2/FORMAT @@ -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 diff --git a/gal2/UCW/Gallery.pm b/gal2/UCW/Gallery.pm index f13889d..847de01 100644 --- a/gal2/UCW/Gallery.pm +++ b/gal2/UCW/Gallery.pm @@ -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 index 0000000..426335e --- /dev/null +++ b/gal2/gal-cache @@ -0,0 +1,85 @@ +#!/usr/bin/perl +# UCW Gallery: Prepare cache +# (c) 2004--2012 Martin Mares + +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); diff --git a/gal2/gal-gen b/gal2/gal-gen index 970e3d2..d8edb57 100755 --- a/gal2/gal-gen +++ b/gal2/gal-gen @@ -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;