1 package Shiar_Sheet::ImagePrep;
9 my ($class, $target) = @_;
10 bless \$target, $class;
14 # copy changed remote url to local file
16 unlink $$target if -e $$target;
17 my $download = shift or return 1;
18 require LWP::UserAgent;
19 my $ua = LWP::UserAgent->new;
21 my $status = $ua->mirror($download, $$target);
23 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
27 my ($imgpath, $thumbpath, $cmds) = @_;
28 if (not -e $$imgpath) {
29 return !-e $thumbpath || unlink $thumbpath;
32 my $xyres = 0 ? '600x400' : '300x200'; # cover
33 my @cmds = @{ $cmds // [] };
34 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
35 # replace option by permillage crop
36 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
37 splice @cmds, $cmdarg, 2, (
38 -set => 'option:distort:viewport' => sprintf(
39 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
40 ($dim[2] || 1) - $dim[0], # width = x2 - x1
41 ($dim[3] || 1) - $dim[1], # height = y2 - y1
42 @dim[0, 1] # offset = x1,y1
44 -distort => SRT => 0, # noop transform to apply viewport
50 -delete => '1--1', -background => 'white',
51 -gravity => defined $cmds ? 'northwest' : 'center',
53 -resize => "$xyres^", -extent => $xyres,
54 '-strip', -quality => '60%', -interlace => 'plane',
60 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
61 "Failed to convert source image.",
63 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),