From 64c6b319030d474f1effc59308775e219f290675 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 3 Feb 2014 00:20:52 +0100 Subject: [PATCH] Cleaned up gal-gen --- gal/FORMAT | 1 + gal/README | 3 ++- gal/bin/gal-gen | 70 ++++++++++++++++++++++++++++++------------------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/gal/FORMAT b/gal/FORMAT index 55ddd0d..e0f776a 100644 --- a/gal/FORMAT +++ b/gal/FORMAT @@ -23,6 +23,7 @@ $meta->{photo}->{$identifier} is a hash of: xf transformation applied w width after scaling h height after scaling + w0, h0 width and height of original image title photo title fmt photo format (png/jpg; defaults to jpg) diff --git a/gal/README b/gal/README index e37fda0..893a6e5 100644 --- a/gal/README +++ b/gal/README @@ -2,7 +2,7 @@ UCW::Gallery v2.0 - (c) 2004--2012 Martin Mares + (c) 2004--2014 Martin Mares ================================================================================ @@ -16,6 +16,7 @@ Perl with the following non-core modules: o UCW::CGI (see http://www.ucw.cz/libucw/) o Image::Magick + o Image::Exif o Archive::Zip o Digest::SHA diff --git a/gal/bin/gal-gen b/gal/bin/gal-gen index 2a1e0ca..22a38ba 100755 --- a/gal/bin/gal-gen +++ b/gal/bin/gal-gen @@ -1,6 +1,6 @@ #!/usr/bin/perl # UCW Gallery: Generate published photos -# (c) 2004--2012 Martin Mares +# (c) 2004--2014 Martin Mares use strict; use warnings; @@ -34,15 +34,11 @@ if (-f $photo_meta) { } my $meta = { 'photo' => {} }; -for my $f (@$orig_list) { - my $id = $f->{id}; +sub get_meta_basic($$$) { + my ($f, $m, $p) = @_; my $rotate = $f->{orientation}; - my $xfrm = $f->{xfrm}; - print "$id: "; - my $p = new Image::Magick; - my $orig = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir')); - my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($orig) or die "Error reading $orig\n"; + my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($f->{orig}) or die "Error reading " . $f->{orig} . "\n"; print "${orig_w}x${orig_h} "; my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h); @@ -60,29 +56,23 @@ for my $f (@$orig_list) { $w = int($w + .5); $h = int($h + .5); - my $m = { - 'o' => $rotate, - 'xf' => $xfrm, - 'w' => $w, - 'h' => $h, - }; - $meta->{photo}->{$id} = $m; + $m->{o} = $rotate; + $m->{xf} = $f->{xfrm}; + $m->{w0} = $w0; + $m->{h0} = $h0; + $m->{w} = $w; + $m->{h} = $h; +} - my $om = $old_meta->{photo}->{$id}; - if ($om && - $om->{o} eq $m->{o} && - $om->{xf} eq $m->{xf} && - $om->{w} eq $m->{w} && - $om->{h} eq $m->{h}) { - print "... uptodate\n"; - next; - } +sub generate_photo($$$) { + my ($f, $m, $p) = @_; my $e; - $e = $p->Read($orig) and die "Error reading $orig: $e"; + $e = $p->Read($f->{orig}) and die "Error reading " . $f->{orig} . ": $e"; $p->Strip; $p->SetAttribute(quality=>90); + my $xfrm = $m->{xf}; if ($xfrm =~ /s/) { print "-> sharpen "; $p->Sharpen(1); @@ -96,6 +86,7 @@ for my $f (@$orig_list) { $p->Normalize(); } + my $rotate = $m->{o}; my $rot = 0; if ($rotate eq "l") { $rot = 270; } elsif ($rotate eq "r") { $rot = 90; } @@ -105,16 +96,40 @@ for my $f (@$orig_list) { $p->Rotate(degrees=>$rot); } - if ($w != $w0 || $h != $h0) { + my ($w, $h) = ($m->{w}, $m->{h}); + if ($w != $m->{w0} || $h != $m->{h0}) { print "-> ${w}x${h} "; $p->Resize(width=>$w, height=>$h); } - my $photo = $gal->photo_name($m, $id); + my $photo = $gal->photo_name($m, $f->{id}); my $tmp = "$photo.new"; $e = $p->Write($tmp) and die "Unable to write $tmp: $e"; rename $tmp, $photo or die "Cannot rename $tmp to $photo: $!\n"; +} + +for my $f (@$orig_list) { + my $id = $f->{id}; + print "$id: "; + + my $m = { }; + $meta->{photo}->{$id} = $m; + $f->{orig} = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir')); + + my $p = new Image::Magick; + get_meta_basic($f, $m, $p); + + my $om = $old_meta->{photo}->{$id}; + if ($om && + $om->{o} eq $m->{o} && + $om->{xf} eq $m->{xf} && + $om->{w} eq $m->{w} && + $om->{h} eq $m->{h}) { + print "... uptodate\n"; + next; + } + generate_photo($f, $m, $p); print "... OK\n"; } @@ -130,3 +145,4 @@ for my $f (<$photo_dir/*.jpg>) { print "Writing meta-data\n"; $gal->write_meta($photo_meta, $meta); +exit 0; -- 2.39.2