From 2adbaeeb5b5bda3af032bf6504367e139147da6c Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Mon, 3 Jan 2022 05:04:12 +0100 Subject: [PATCH] word/edit: method generate() to create thumbnail variants --- Shiar_Sheet/ImagePrep.pm | 19 +++++++++++++------ tools/mkwordthumb | 4 +--- word/edit.plp | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Shiar_Sheet/ImagePrep.pm b/Shiar_Sheet/ImagePrep.pm index f76cb31..b5038cf 100644 --- a/Shiar_Sheet/ImagePrep.pm +++ b/Shiar_Sheet/ImagePrep.pm @@ -3,7 +3,7 @@ package Shiar_Sheet::ImagePrep; use 5.014; use warnings; -our $VERSION = '1.01'; +our $VERSION = '1.02'; sub new { my ($class, $target) = @_; @@ -33,16 +33,23 @@ sub dimensions { return split /\s/, $xy, 3; } -sub convert { - my ($imgpath, $thumbpath, $cmds, $xyres) = @_; +sub generate { + my ($imgpath, $thumbpath, $cmds) = @_; if (not -e $$imgpath) { return !-e $thumbpath || unlink $thumbpath; } + $cmds //= []; + $imgpath->convert($thumbpath, $cmds, '300x200') and # low-res cover + $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r, + $cmds, '600x400' # higher dpi + ); +} +sub convert { + my ($imgpath, $thumbpath, $cmds, $xyres) = @_; #my ($w, $h) = $imgpath->dimensions; - $xyres //= '300x200'; # cover - my $aspect = 3/2; - my @cmds = @{ $cmds // [] }; + #my $aspect = 3/2; # $xyres + my @cmds = @{$cmds}; if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) { # replace option by permillage crop my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1]; diff --git a/tools/mkwordthumb b/tools/mkwordthumb index 8e7909f..b4e883c 100755 --- a/tools/mkwordthumb +++ b/tools/mkwordthumb @@ -17,8 +17,6 @@ while (my $row = $query->hash) { eval { my $meta = eval { JSON->new->decode($row->{image} // '{}') } or die ["Invalid JSON metadata in image column.", $@]; - my $basename = "data/word/en/$row->{id}"; - $image->convert("$basename.jpg", $meta->{convert}); - $image->convert("$basename.webp", $meta->{convert}, '600x400'); + $image->generate("data/word/en/$row->{id}.jpg", $meta->{convert}); } or warn "$row->{id}: @{$@}"; } diff --git a/word/edit.plp b/word/edit.plp index 8b9311c..7ba90fa 100644 --- a/word/edit.plp +++ b/word/edit.plp @@ -218,7 +218,7 @@ elsif (defined $post{form}) {{ $reimage++ if $fields{rethumb}; # force refresh if ($reimage) { eval { - $image->convert($wordcol{convert}->{-src}->($row), $imagecol->{convert}); + $image->generate($wordcol{convert}->{-src}->($row), $imagecol->{convert}); } or do { my ($warn, @details) = ref $@ ? @{$@} : $@; Alert([ "Thumbnail image not generated", $warn ], @details); -- 2.30.0