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";
30 [identify => -format => '%w %h', $$imgpath],
31 '<' => \undef, '>&' => \my $xy
32 ) or die ["Image dimensions could not be determined.", $$imgpath];
33 return split /\s/, $xy, 3;
37 my ($imgpath, $thumbpath, $cmds) = @_;
38 if (not -e $$imgpath) {
39 return !-e $thumbpath || unlink $thumbpath;
42 #my ($w, $h) = $imgpath->dimensions;
43 my $xyres = 0 ? '600x400' : '300x200'; # cover
45 my @cmds = @{ $cmds // [] };
46 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
47 # replace option by permillage crop
48 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
49 $dim[$_] ||= 1 for 2, 3; # optional end
50 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
51 splice @cmds, $cmdarg, 2, (
52 #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
53 #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
54 -set => 'option:distort:viewport' => sprintf(
55 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
56 "w*$dim[4]", "h*$dim[5]", # width x height
57 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
58 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
59 "w*$dim[0]", "h*$dim[1]", # x+y offset
61 -distort => SRT => 0, # noop transform to apply viewport
67 -delete => '1--1', -background => 'white',
68 -gravity => defined $cmds ? 'northwest' : 'center',
70 -resize => "$xyres^", -extent => $xyres,
71 '-strip', -quality => '60%', -interlace => 'plane',
77 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
78 "Failed to convert source image.",
80 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),