dieren: generate images using common ImagePrep module
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 3 Jan 2024 22:50:47 +0000 (23:50 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 3 Jan 2024 22:50:47 +0000 (23:50 +0100)
Code cleanup and support for custom -area parameters.
Identical results.

Shiar_Sheet/ImagePrep.pm
tools/mkimgthumb

index 04a4270f5ac20cb87ce2be362bbfdd8d81bf3473..328cb4f361b3bd69387cf7fe80edd45ffd639eed 100644 (file)
@@ -4,7 +4,7 @@ use 5.020;
 use warnings;
 use experimental 'signatures';
 
-our $VERSION = '1.02';
+our $VERSION = '1.03';
 
 sub new ($class, $target) {
        bless \$target, $class;
@@ -43,7 +43,7 @@ sub generate ($imgpath, $thumbpath, $opt) {
        );
 }
 
-sub convert ($imgpath, $thumbpath, $cmds, $xyres) {
+sub convert ($imgpath, $thumbpath, $cmds, $xyres = 0) {
        #my ($w, $h) = $imgpath->dimensions;
        #my $aspect = 3/2; # $xyres
        my @cmds = @{$cmds};
@@ -72,7 +72,7 @@ sub convert ($imgpath, $thumbpath, $cmds, $xyres) {
                '-strip', -quality => '60%', -interlace => 'plane',
                -gravity => defined $cmds ? 'northwest' : 'center',
                @cmds,
-               -resize => "$xyres^", -extent => $xyres,
+               $xyres ? (-resize => "$xyres^", -extent => $xyres) : (),
                $thumbpath
        );
 
index 217b7734d45ec55c2d2100bb314645ade9ace804..8f0097abba417291cdb3bd5bf682c29e2302fb15 100755 (executable)
@@ -1,6 +1,8 @@
 #!/usr/bin/env perl
 use 5.014;
 use warnings;
+use lib $0 =~ s{[^/]+$}{..}r; # project root
+use Shiar_Sheet::ImagePrep '1.03';
 
 my $failcount = 0;
 
@@ -21,13 +23,18 @@ for my $src (@ARGV) {
                        if @crop > 2;
                unshift @cmds, -chop => "$crop[0]%x$crop[1]%";
        }
-       unshift @cmds, -gravity => 'northwest' if @cmds;
-       push @cmds, '-strip', -gravity => 'north', -quality => 60;
+       push @cmds, -gravity => 'north';
        my @cmdwebp = (-resize => '630x420^', -extent => '630x420', -quality => 30);
        my @cmdjpeg = (-resize => '300x200^', -extent => '300x200');
-       system(convert => $src, @cmds, @cmdjpeg, "../$name.jpg" ) == 0 and
-       system(convert => $src, @cmds, @cmdwebp, "../$name.webp") == 0
-               or $failcount += warn "error creating $name.jpg from $src\n";
+       eval {
+               my $image = Shiar_Sheet::ImagePrep->new($src);
+               $image->convert("../$name.jpg",  [@cmds, @cmdjpeg]);
+               $image->convert("../$name.webp", [@cmds, @cmdwebp]);
+       } or do {
+               warn "error creating image:\n";
+               warn ref $@ eq 'ARRAY' ? $@->[1] : $@ if $@;
+               $failcount++;
+       };
 }
 
 exit $failcount;