1 package Shiar_Sheet::ImagePrep;
5 use experimental 'signatures';
9 sub new ($class, $target) {
10 bless \$target, $class;
13 sub download ($target, $download) {
14 # copy changed remote url to local file
15 unlink $$target if -e $$target;
16 defined $download or return 1;
17 require LWP::UserAgent;
18 my $ua = LWP::UserAgent->new;
20 my $status = $ua->mirror($download, $$target);
22 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
25 sub dimensions ($imgpath) {
28 [identify => -format => '%w %h', $$imgpath],
29 '<' => \undef, '>&' => \my $xy
30 ) or die ["Image dimensions could not be determined.", $$imgpath];
31 return split /\s/, $xy, 3;
34 sub generate ($imgpath, $thumbpath, $cmds) {
35 if (not -e $$imgpath) {
36 return !-e $thumbpath || unlink $thumbpath;
39 $imgpath->convert($thumbpath, $cmds, '300x200') and # low-res cover
40 $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r,
41 [@{$cmds}, -quality => 40], '600x400' # higher dpi tradeoff
45 sub convert ($imgpath, $thumbpath, $cmds, $xyres) {
46 #my ($w, $h) = $imgpath->dimensions;
47 #my $aspect = 3/2; # $xyres
49 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
50 # replace option by permillage crop
51 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
52 $dim[$_] ||= 1 for 2, 3; # optional end
53 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
54 splice @cmds, $cmdarg, 2, (
55 #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
56 #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
57 -set => 'option:distort:viewport' => sprintf(
58 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
59 "w*$dim[4]", "h*$dim[5]", # width x height
60 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
61 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
62 "w*$dim[0]", "h*$dim[1]", # x+y offset
64 -distort => SRT => 0, # noop transform to apply viewport
70 -delete => '1--1', -background => 'white',
71 '-strip', -quality => '60%', -interlace => 'plane',
72 -gravity => defined $cmds ? 'northwest' : 'center',
74 -resize => "$xyres^", -extent => $xyres,
78 $imgpath->runcommand(@cmds);
81 sub runcommand ($, @cmds) {
84 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
85 "Failed to convert source image.",
87 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),