chiark / gitweb /
mason/.perl-lib/TrivGal.pm (Image::scale): Check size much earlier.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 20 Jun 2023 20:19:09 +0000 (21:19 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 09:18:36 +0000 (10:18 +0100)
We check whether the original image is small enough to be used as-is
rather than making a scaled-down version, but currently we do that very
late, when we're actually about to try scaling it.  That's obviously a
bad idea.  Instead, check early now that we have a library that can find
an image's size without costly decoding.  In particular, this is now
done /before/ we decide whether to hand off via the `?scale=...'  lazy
thumbnailer, which is important because, in the case where the image is
small, we'll never actually make the thumbnail and we'll always pay the
redirection penalty.

This will actually slow down handling of large images; but they're kinda
inevitably a bit slow.  Besides, `Image::Size' is surprisingly fast.

mason/.perl-lib/TrivGal.pm

index d6d3afb4ce32fa90f5c2e9a235764937c707bc2d..513a68262f813b8f3cce4875feea124a48cb5a21 100644 (file)
@@ -240,6 +240,9 @@ package TrivGal::Image {
 
     my $path = $me->{path};
     my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
+    if ($me->sz <= $sz)
+      { return $m->interp->apply_escapes("$IMGURL/$path", "u"); }
+
     my $thumb = "$CACHE/scale.$sz/$path";
     my $thumburl =
       $m->interp->apply_escapes("$CACHEURL/scale.$sz/$path", "u");
@@ -265,8 +268,6 @@ package TrivGal::Image {
       }
     }
 
-    if ($me->sz <= $sz)
-      { return $m->interp->apply_escapes("$IMGURL/$path", "u"); }
     my $sc = $sz/$me->sz;
     my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);