-export qw{scaled};
-sub scaled ($$) {
- my ($scale, $path) = @_;
-
- my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
- my $imgpath = "$IMGROOT/$path";
- my $ist = stat $imgpath or die "no image `$path'";
- my $thumb = "$CACHE/scaled.$scale/$path";
- my $thumburl = "$CACHEURL/scaled.$scale/$path";
- my $tst = stat $thumb;
- if (defined $tst && $tst->mtime > $ist->mtime) { return $thumburl; }
- my ($dir, $base, $ext) = split_path $thumb;
- my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
-
- my $img = Image::Imlib2->load($imgpath);
- 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;
+my %ORIENT =
+ (1 => [0, 0],
+ 2 => [0, 1],
+ 3 => [2, 0],
+ 4 => [2, 1],
+ 5 => [3, 1],
+ 6 => [1, 0],
+ 7 => [1, 1],
+ 8 => [3, 0]);
+
+package TrivGal::Image {
+ use File::Path qw{make_path};
+ use File::stat;
+
+ sub new ($$) {
+ my ($cls, $path) = @_;
+ my $imgpath = "$IMGROOT/$path";
+ my $st = stat $imgpath or die "no image `$path'";
+ return bless {
+ path => $path, imgpath => $imgpath,
+ mtime => $st->mtime,
+ 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 $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;
+ }
+ }
+
+ return $url;
+ }