word/edit: method generate() to create thumbnail variants
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 3 Jan 2022 04:04:12 +0000 (05:04 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 7 Feb 2022 17:42:33 +0000 (18:42 +0100)
Shiar_Sheet/ImagePrep.pm
tools/mkwordthumb
word/edit.plp

index f76cb31d7f4f3a448d91c6799c9fc8277a18a19a..b5038cf248bcfe6dc0a62d7c730dec27f0328d32 100644 (file)
@@ -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];
index 8e7909f5daf0fe2be762cd82c7143603ebf3cedf..b4e883cde9266c1347d0a08618bc185306578bf3 100755 (executable)
@@ -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}: @{$@}";
 }
index 8b9311c3db4200cc6c49463bebe53a42c30c03dd..7ba90faa47eb4473f0b50f07726f8b35fcc8565c 100644 (file)
@@ -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);