X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/tgal/blobdiff_plain/603ed7abab9d5296be22dcf90b75313793b19a7d..e9f8c99ed456dc006c02e2dbde1568b5bb04476b:/mason/.perl-lib/TrivGal.pm diff --git a/mason/.perl-lib/TrivGal.pm b/mason/.perl-lib/TrivGal.pm index e6bfcf4..2bd64ed 100644 --- a/mason/.perl-lib/TrivGal.pm +++ b/mason/.perl-lib/TrivGal.pm @@ -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. @@ -148,10 +121,19 @@ our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/"; export qw{%SIZE}; our %SIZE = (smallthumb => 96, medthumb => 144, - bigthumb => 228, + bigthumb => 216, + tiny => 320, small => 480, embed => 720, - view => 1200); + 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; @@ -217,6 +199,8 @@ package TrivGal::Image { path => $path, imgpath => $imgpath, mtime => $st->mtime, img => undef, + rot => undef, flip => undef, + wd => undef, ht => undef, _wd => undef, _ht => undef, sz => undef }, $cls; @@ -232,7 +216,29 @@ package TrivGal::Image { @$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) = @_; @@ -259,27 +265,16 @@ package TrivGal::Image { } else { my $img = $me->{img}; unless (defined $img) { - my $exif = new Image::ExifTool; - $exif->ExtractInfo($me->{imgpath}); - my $orient = $exif->GetValue("Orientation", "ValueConv"); - $img = $me->{img} = Image::Imlib2->load($me->{imgpath}); - if (defined $orient) { - my ($rot, $flip) = @{$ORIENT{$orient}}; - if ($rot) { $img->image_orientate($rot); } - if ($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;