From 4e74ddf4ab1a4c3a7a3b9936a978f225a690aa7d Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 24 Dec 2021 17:28:03 +0000 Subject: [PATCH] mason/{.perl-lib/TrivGal,dhandler}: Make an image object. The objective here is to separate image loading from scaling, so that we can make several scaled output images without having to load and decode the original image each time. The `.thumbnail' module now takes an image object rather than a pathname. There's no functional change. --- mason/.perl-lib/TrivGal.pm | 79 ++++++++++++++++++++++++++++------------------ mason/dhandler | 15 +++++---- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/mason/.perl-lib/TrivGal.pm b/mason/.perl-lib/TrivGal.pm index 9822a0b..838de97 100644 --- a/mason/.perl-lib/TrivGal.pm +++ b/mason/.perl-lib/TrivGal.pm @@ -29,7 +29,6 @@ use autodie qw{:all}; use Errno; use Exporter qw{import}; -use File::Path qw{make_path}; use File::stat; use Image::Imlib2; use User::pwent; @@ -183,35 +182,55 @@ sub clean_temp_files () { ###-------------------------------------------------------------------------- ### Scaled images. -export qw{scaled}; -sub scaled ($$) { - my ($scale, $path) = @_; - - my $sz = $SIZE{$scale} or die "unknown scale `$scale'"; - my $imgpath = "$IMGROOT/$path"; - my $ist = stat $imgpath or die "no image `$path'"; - my $thumb = "$CACHE/scaled.$scale/$path"; - my $thumburl = "$CACHEURL/scaled.$scale/$path"; - my $tst = stat $thumb; - if (defined $tst && $tst->mtime > $ist->mtime) { return $thumburl; } - my ($dir, $base, $ext) = split_path $thumb; - my $ty = $TYPE{lc $ext} or die "unknown type `$ext'"; - - my $img = Image::Imlib2->load($imgpath); - my ($wd, $ht) = ($img->width, $img->height); - my $max = $wd > $ht ? $wd : $ht; - if ($max <= $sz) { return "$IMGURL/$path"; } - my $sc = $sz/$max; - my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht); - - $scaled->image_set_format($ty->imlibfmt); - $scaled->set_quality(90); - my $new = "$TMP/t${$}$ext"; - make_path $TMP; - $scaled->save($new); - make_path $dir; - rename $new, $thumb; - return $thumburl; +package TrivGal::Image { + use File::Path qw{make_path}; + use File::stat; + + sub new ($$) { + my ($cls, $path) = @_; + my $imgpath = "$IMGROOT/$path"; + my $st = stat $imgpath or die "no image `$path'"; + return bless { + path => $path, + mtime => $st->mtime, + img => undef + }, $cls; + } + + sub scale ($$) { + my ($me, $scale) = @_; + + my $path = $me->{path}; + my $sz = $SIZE{$scale} or die "unknown scale `$scale'"; + my $thumb = "$CACHE/scale.$sz/$path"; + my $thumburl = "$CACHEURL/scale.$sz/$path"; + my $st = stat $thumb; + if (defined $st && $st->mtime > $me->{mtime}) { return $thumburl; } + + my ($dir, $base, $ext) = TrivGal::split_path $thumb; + my $ty = $TYPE{lc $ext} or die "unknown type `$ext'"; + + my $img = $me->{img}; + unless (defined $img) { + my $imgpath = "$IMGROOT/$path"; + $img = $me->{img} = Image::Imlib2->load($imgpath); + } + + my ($wd, $ht) = ($img->width, $img->height); + my $max = $wd > $ht ? $wd : $ht; + if ($max <= $sz) { return "$IMGURL/$path"; } + my $sc = $sz/$max; + my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht); + + $scaled->image_set_format($ty->imlibfmt); + $scaled->set_quality(90); + my $new = "$TMP/t${$}$ext"; + make_path $TMP; + $scaled->save($new); + make_path $dir; + rename $new, $thumb; + return $thumburl; + } } ###-------------------------------------------------------------------------- diff --git a/mason/dhandler b/mason/dhandler index 44a9ceb..7d76fdf 100755 --- a/mason/dhandler +++ b/mason/dhandler @@ -100,9 +100,9 @@ Failed to find ‘<% $path |h %>’.