mason/dhandler (.image): Generate thumbnails more memory-efficiently.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 12 Jun 2023 10:37:17 +0000 (11:37 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 12 Jun 2023 10:48:39 +0000 (11:48 +0100)
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.

mason/dhandler

index a48610c..558da02 100755 (executable)
@@ -149,20 +149,29 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 <%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 &lsquo;<% $path |h %>&rsquo;.
 % }
 </div>
 %
-% my %img = map { $_ => TrivGal::Image->new($dir . "/" . $_->name) } @$ff;
 % for my $size (qw{smallthumb medthumb bigthumb}) {
 <div class="thumbstrip <% $size %>">
 %   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 &>\
 %   }