chiark / gitweb /
mason/.perl-lib/TrivGal.pm: Simplify the image-type machinery.
[tgal] / mason / .perl-lib / TrivGal.pm
index 4e5527bb3c3a30f2721fa9f40d4c5bd68da6bfd8..2bd64edcd9ff0c83adcced28d54aad0976e47fd2 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;
@@ -93,33 +93,6 @@ sub urldecode ($) {
   return $u;
 }
 
-###--------------------------------------------------------------------------
-### Image types.
-
-our %TYPE;
-
-package TrivGal::ImageType {
-  sub new ($$) {
-    my ($cls, $ext) = @_;
-    return $TYPE{$ext} = bless { ext => $ext }, $cls;
-  }
-  sub ext ($) {
-    my ($me, @args) = @_;
-    return $me->{ext};
-  }
-  sub mimetype ($@) {
-    my ($me, @args) = @_;
-    return TrivGal::read_or_set $me, $me->{mimetype}, @args;
-  }
-  sub imlibfmt ($@) {
-    my ($me, @args) = @_;
-    return TrivGal::read_or_set $me, $me->{imlibfmt}, @args;
-  }
-}
-
-TrivGal::ImageType->new(".jpg")->mimetype("image/jpeg")->imlibfmt("jpeg");
-TrivGal::ImageType->new(".png")->mimetype("image/png")->imlibfmt("png");
-
 ###--------------------------------------------------------------------------
 ### Configuration.
 
@@ -159,6 +132,9 @@ our %SIZE = (smallthumb => 96,
             vast => 5400,
             immense => 8100);
 
+export qw{%TYPE};
+our %TYPE = map { $_ => 1 } qw{.jpg .png};
+
 export qw{init};
 my $initp = 0;
 sub init () {
@@ -259,6 +235,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 +265,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;