dieren: mkimgthumb script options to override export formats
[sheet.git] / tools / mkimgthumb
index 217b7734d45ec55c2d2100bb314645ade9ace804..32ae1a3f2b6317511547ac728f5d6fa041f87263 100755 (executable)
@@ -1,6 +1,34 @@
 #!/usr/bin/env perl
 use 5.014;
 use warnings;
+use lib $0 =~ s{[^/]+$}{..}r; # project root
+use Shiar_Sheet::ImagePrep '1.03';
+
+our $VERSION = '1.00';
+
+my %opt;
+
+if (@ARGV and $ARGV[0] =~ /^-/) {
+       require Getopt::Long;
+       Getopt::Long->import(qw( 2.33 :config gnu_getopt ));
+       GetOptions(\%opt,
+               'jpg=s',
+               'webp=s',
+       ) or exit 64;
+}
+
+%opt or %opt = (
+       jpg  => '300x200',
+       webp => '630x420@30',
+);
+
+my @ffs;
+for (keys %opt) {
+       push @ffs, my $ff = [$_];
+       my $r = $opt{$_};
+       push @{$ff}, -quality => $1 if $r =~ s/@(\d+)//;
+       push @{$ff}, -resize => "$r^", -extent => $r;
+}
 
 my $failcount = 0;
 
@@ -21,13 +49,19 @@ 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;
-       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";
+       push @cmds, -gravity => 'north';
+       eval {
+               my $image = Shiar_Sheet::ImagePrep->new($src);
+               for (@ffs) {
+                       my ($ff, @ffcmds) = @{$_};
+                       $image->convert("../$name.$ff", [@cmds, @ffcmds]);
+               }
+               1;
+       } or do {
+               warn "error creating image:\n";
+               warn ref $@ eq 'ARRAY' ? $@->[1] : $@ if $@;
+               $failcount++;
+       };
 }
 
 exit $failcount;