From c4af187b7b7518a0810e90f8fd79caba84ac0d34 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Wed, 3 Jan 2024 23:50:47 +0100 Subject: [PATCH] dieren: generate images using common ImagePrep module Code cleanup and support for custom -area parameters. Identical results. --- Shiar_Sheet/ImagePrep.pm | 6 +++--- tools/mkimgthumb | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Shiar_Sheet/ImagePrep.pm b/Shiar_Sheet/ImagePrep.pm index 04a4270..328cb4f 100644 --- a/Shiar_Sheet/ImagePrep.pm +++ b/Shiar_Sheet/ImagePrep.pm @@ -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 ); diff --git a/tools/mkimgthumb b/tools/mkimgthumb index 217b773..8f0097a 100755 --- a/tools/mkimgthumb +++ b/tools/mkimgthumb @@ -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; -- 2.30.0