mason/.perl-lib/TrivGal.pm: Use GraphicsMagick rather than Imlib2.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 10:14:48 +0000 (11:14 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 10:14:48 +0000 (11:14 +0100)
The most important difference is that GM preserves EXIF data.  This
means we don't have to explicitly rotate and/or flip the image.

mason/.perl-lib/TrivGal.pm

index 4e5527b..f498894 100644 (file)
@@ -30,8 +30,8 @@ use autodie qw{:all};
 use Errno;
 use Exporter qw{import};
 use File::stat;
+use Graphics::Magick;
 use Image::ExifTool qw{};
-use Image::Imlib2;
 use Image::Size qw{};
 use User::pwent;
 use POSIX;
@@ -259,6 +259,11 @@ package TrivGal::Image {
   sub wd ($) { my ($me) = @_; $me->_getexif; return $me->{wd}; }
   sub ht ($) { my ($me) = @_; $me->_getexif; return $me->{ht}; }
 
+  sub _check_gm ($) {
+    my ($rc) = @_;
+    "$rc" and die "failed to hack `$me->{img}': $rc";
+  }
+
   sub scale ($$;$) {
     my ($me, $scale, $forcep) = @_;
     my $m = HTML::Mason::Request->instance;
@@ -284,22 +289,16 @@ package TrivGal::Image {
       } else {
        my $img = $me->{img};
        unless (defined $img) {
-         $me->_getexif;
-         $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
-         if ($me->{rot}) { $img->image_orientate($me->{rot}); }
-         if ($me->{flip}) { $img->flip_horizontal(); }
+         $img = $me->{img} = Graphics::Magick->new;
+         _check_gm $img->Read($me->{imgpath});
        }
 
        my ($dir, undef, $ext) = TrivGal::split_path $thumb;
-       my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
-       my $sc = $sz/$me->sz;
-       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";
+       $img = $img->Clone;
+       my $new = "$TMP/t$$-thumb$ext";
+       _check_gm $img->Resize(geometry => $sz);
        make_path $TMP, { mode => 0771 };
-       $scaled->save($new);
+       _check_gm $img->Write($new);
        make_path $dir, { mode => 0771 };
        rename $new, $thumb;
        $url = $thumburl;