index: release v1.18 with only altgr index linked
[sheet.git] / tools / mkimgthumb
index 8875252c214193e7328a5db50f0a7915eaefe152..e9d6eb2159286003a9ccc6c28412334d2d01ee70 100755 (executable)
@@ -1,22 +1,76 @@
 #!/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 => !/\dx\d+$/ ? $_ : ("$_^", -extent => $_)
+               for split / /, $r;
+}
+
+my $target = '..';
+$target = pop @ARGV if @ARGV >= 2 and -d $ARGV[-1];
 
 my $failcount = 0;
 
 for my $src (@ARGV) {
-       my ($name, @cmds) = split /:/, $src =~ s/\.(\w+)\z//r;
+       my ($name, @cmds) = split /:(?<!\\:)/, $src =~ s/\.(\w+)\z//r;
        my $ext = $1 // '*';
+       print $name;
        next if $name =~ m/\./;
        unless (-e $src) {
                ($src) = grep {-e} glob qq<"$name"{,:*}.$ext> or next;
        }
-       say $name;
-       unshift @cmds, -gravity => 'northwest';
-       push @cmds, -resize => '300x200^', -gravity => 'north', -extent => '300x200';
-       push @cmds, '-strip', -quality => '60%';
-       system(convert => @cmds, $src => "../$name.jpg") == 0
-               or $failcount += warn "error creating $name.jpg from $src\n";
+       s/\\(.)/$1/g for @cmds;
+       print ':';
+
+       if (@cmds and $cmds[0] =~ /^\d/) {
+               # crop shorthand from initial dimension argument
+               my @crop = split /\D/, shift @cmds;
+               unshift @cmds, -gravity => 'southeast', -chop => "$crop[2]%x$crop[3]%"
+                       if @crop > 2;
+               unshift @cmds, -chop => "$crop[0]%x$crop[1]%";
+       }
+       push @cmds, -gravity => 'north';
+       eval {
+               my $image = Shiar_Sheet::ImagePrep->new($src);
+               for (@ffs) {
+                       my ($ff, @ffcmds) = @{$_};
+                       print " $ff";
+                       $image->convert("$target/$name.$ff", [@cmds, @ffcmds]);
+               }
+               1;
+       } or do {
+               say ' FAILED';
+               warn ref $@ eq 'ARRAY' ? $@->[1] : $@ if $@;
+               $failcount++;
+       };
+}
+continue {
+       say '';
 }
 
 exit $failcount;