chiark / gitweb /
mason/.perl-lib/TrivGal.pm: Simplify the image-type machinery.
[tgal] / mason / .perl-lib / TrivGal.pm
index b47835a10c806727c776e7bc8db5dda38591e06f..2bd64edcd9ff0c83adcced28d54aad0976e47fd2 100644 (file)
@@ -30,8 +30,9 @@ 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;
 
@@ -92,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.
 
@@ -147,8 +121,19 @@ our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/";
 export qw{%SIZE};
 our %SIZE = (smallthumb => 96,
             medthumb => 144,
-            bigthumb => 228,
-            view => 1200);
+            bigthumb => 216,
+            tiny => 320,
+            small => 480,
+            embed => 720,
+            medium => 1080,
+            big => 1600,
+            large => 2400,
+            huge => 3600,
+            vast => 5400,
+            immense => 8100);
+
+export qw{%TYPE};
+our %TYPE = map { $_ => 1 } qw{.jpg .png};
 
 export qw{init};
 my $initp = 0;
@@ -211,58 +196,92 @@ package TrivGal::Image {
     my $imgpath = "$IMGROOT/$path";
     my $st = stat $imgpath or die "no image `$path'";
     return bless {
-      path => $path,
+      path => $path, imgpath => $imgpath,
       mtime => $st->mtime,
-      img => undef
+      img => undef,
+      rot => undef, flip => undef,
+      wd => undef, ht => undef,
+      _wd => undef, _ht => undef,
+      sz => undef
     }, $cls;
   }
 
+  sub _getsz ($) {
+    my ($me) = @_;
+    return if defined $me->{_wd};
+
+    my ($wd, $ht, $err) = Image::Size::imgsize $me->{imgpath};
+    defined $wd or die "failed to read size of `$me->{path}': $err";
+    my $sz = $wd; if ($sz < $ht) { $sz = $ht; }
+    @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
+  }
+
+  sub _getexif ($) {
+    my ($me) = @_;
+    return if defined $me->{wd};
+
+    $me->_getsz;
+    my $exif = new Image::ExifTool; $exif->ExtractInfo($me->{imgpath});
+    my $orient = $exif->GetValue("Orientation", "ValueConv");
+    my ($wd, $ht) = @$me{"_wd", "_ht"};
+    my ($rot, $flip);
+    if (defined $orient) { ($rot, $flip) = $ORIENT{$orient}->@*; }
+    else { ($rot, $flip) = (0, 0); }
+    if ($rot%2) { ($wd, $ht) = ($ht, $wd); }
+    @$me{"rot", "flip", "wd", "ht"} = ($rot, $flip, $wd, $ht);
+  }
+
+  sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; }
+  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;
 
     my $path = $me->{path};
     my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
-    my $thumb = "$CACHE/scale.$sz/$path";
-    my $thumburl =
-      $m->interp->apply_escapes("$CACHEURL/scale.$sz/$path", "u");
-    my $st = stat $thumb;
-    if (defined $st && $st->mtime > $me->{mtime}) { return $thumburl; }
-    return
-      $m->interp->apply_escapes("$SCRIPTURL/$path", "u") . "?scale=$scale"
-      unless $forcep;
-
-    my ($dir, $base, $ext) = TrivGal::split_path $thumb;
-    my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
-
-    my $img = $me->{img};
-    unless (defined $img) {
-      my $imgpath = "$IMGROOT/$path";
-      my $exif = new Image::ExifTool;
-      $exif->ExtractInfo($imgpath);
-      my $orient = $exif->GetValue("Orientation", "ValueConv");
-      $img = $me->{img} = Image::Imlib2->load($imgpath);
-      if (defined $orient) {
-       my ($rot, $flip) = @{$ORIENT{$orient}};
-       if ($rot) { $img->image_orientate($rot); }
-       if ($flip) { $img->flip_horizontal(); }
+    my $url;
+
+    if ($me->sz <= $sz)
+      { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); }
+    else {
+      my $tail = "scale.$sz/$path";
+      my $thumb = "$CACHE/$tail";
+      my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
+
+      my $st = stat $thumb;
+      if ($st && $st->mtime > $me->{mtime})
+       { $url = $thumburl; }
+      elsif (!$forcep) {
+       $url =
+         $m->interp->apply_escapes("$SCRIPTURL/$path", "u") .
+         "?scale=$scale";
+      } else {
+       my $img = $me->{img};
+       unless (defined $img) {
+         $img = $me->{img} = Graphics::Magick->new;
+         _check_gm $img->Read($me->{imgpath});
+       }
+
+       my ($dir, undef, $ext) = TrivGal::split_path $thumb;
+       $img = $img->Clone;
+       my $new = "$TMP/t$$-thumb$ext";
+       _check_gm $img->Resize(geometry => $sz);
+       make_path $TMP, { mode => 0771 };
+       _check_gm $img->Write($new);
+       make_path $dir, { mode => 0771 };
+       rename $new, $thumb;
+       $url = $thumburl;
       }
     }
 
-    my ($wd, $ht) = ($img->width, $img->height);
-    my $max = $wd > $ht ? $wd : $ht;
-    if ($max <= $sz) { return "$IMGURL/$path"; }
-    my $sc = $sz/$max;
-    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";
-    make_path $TMP;
-    $scaled->save($new);
-    make_path $dir;
-    rename $new, $thumb;
-    return $thumburl;
+    return $url;
   }
 }
 
@@ -304,12 +323,13 @@ sub listdir ($) {
       } else {
        if ($item && $comment) { $item->comment($comment); }
        my ($flags, $name, $c) =
-         /^ (?: ([!]+) \s+)?           # flags
+         /^ (?: ([-!]+) \s+)?          # flags
             (\S+) \s*                  # filename
             (\S | \S.*\S )?            # start of the comment
             \s*
             $/x;
        my $indexp = $flags =~ /!/;
+       my $hidep = $flags =~ /-/;
        $name = urldecode $name;
        my $list;
        $item = TrivGal::Item->new($name);
@@ -318,7 +338,7 @@ sub listdir ($) {
          die "can't index a folder" if $indexp;
        } else {
          $list = \@f;
-         my ($dir, $base, $ext) = split_path $name;
+         my (undef, undef, $ext) = split_path $name;
          die "unknown image type" unless $TYPE{lc $ext};
          if ($indexp) {
            die "two index images" if defined $ix;
@@ -326,7 +346,7 @@ sub listdir ($) {
          }
        }
        $comment = $c;
-       push @$list, $item;
+       push @$list, $item unless $hidep;
       }
     }
     if ($item && $comment) { $item->comment($comment); }
@@ -340,7 +360,7 @@ sub listdir ($) {
     closedir $d;
 
     ENT: for my $e (sort @e) {
-      my ($dir, $base, $ext) = split_path $e;
+      my (undef, undef, $ext) = split_path $e;
       my $dotp = $e =~ /^\./;
       my $st = stat "$path/$e";
       my $list = undef;