From: Mark Wooding Date: Mon, 12 Jun 2023 10:37:17 +0000 (+0100) Subject: mason/dhandler (.image): Generate thumbnails more memory-efficiently. X-Git-Url: https://git.distorted.org.uk/~mdw/tgal/commitdiff_plain/bfc5bfe6967009433043d3aee1d74eef4b8cec8f mason/dhandler (.image): Generate thumbnails more memory-efficiently. As threatened in the previous commit, we now load each image once, generate all of the necessary rescaled versions, and move on. This includes the main view image. --- diff --git a/mason/dhandler b/mason/dhandler index a48610c..558da02 100755 --- a/mason/dhandler +++ b/mason/dhandler @@ -149,20 +149,29 @@ Failed to find ‘<% $path |h %>’. <%def .image>\ <%perl> my ($dir, $base, $ext) = split_path $path; - my $real = join_paths $IMGROOT, $path; - my $img = TrivGal::Image->new($path); - if (defined $scale) { $m->redirect($img->scale($scale)); } + if (defined $scale) { + my $img = TrivGal::Image->new($path); + $m->redirect($img->scale($scale)); + } + my $real = join_paths $IMGROOT, $path; my $url = join_paths $IMGURL, $path; my $realdir = join_paths $IMGROOT, $dir; my $urldir = join_paths $SCRIPTURL, $dir; my ($dd, $ff, $ii) = listdir $realdir; - my $vw = $img->scale("view"); + my @thumbsz = qw{smallthumb medthumb bigthumb}; + my %tn; + my $vw; my $fi = undef; - FILE: for (my $i = 0; $i < @$ff; $i++) - { if ($ff->[$i]->name eq "$base$ext") { $fi = $i; last FILE; } } + FILE: for (my $i = 0; $i < @$ff; $i++) { + my $f = $ff->[$i]; + my $img = TrivGal::Image->new(join_paths $dir, $f->name); + for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); } + if ($ff->[$i]->name eq "$base$ext") + { $fi = $i; $vw = $img->scale("view"); } + } defined $fi or die "image not found in its folder?"; my $this = $ff->[$fi]; @@ -208,12 +217,11 @@ Failed to find ‘<% $path |h %>’. % } % -% my %img = map { $_ => TrivGal::Image->new($dir . "/" . $_->name) } @$ff; % for my $size (qw{smallthumb medthumb bigthumb}) {
% for my $f (@$ff) { <& .thumbnail, target => $f->name, - img => $img{$f}->scale($size), size => $size, + tn => $tn{$f->name}{$size}, size => $size, caption => $m->interp->apply_escapes($f->name, "h"), focus => $f eq $this &>\ % }