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, $opt) {
35 if (not -e $$imgpath) {
36 return !-e $thumbpath || unlink $thumbpath;
38 my @cmds = @{$opt->{convert} // []};
39 unshift @cmds, -area => $_ for $opt->{crop32} || ();
40 $imgpath->convert($thumbpath, \@cmds, '300x200') and # low-res cover
41 $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r,
42 [@cmds, -quality => 40], '600x400' # higher dpi tradeoff
46 sub convert ($imgpath, $thumbpath, $cmds, $xyres = 0) {
47 #my ($w, $h) = $imgpath->dimensions;
48 #my $aspect = 3/2; # $xyres
50 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
51 # replace option by permillage crop
52 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
53 $dim[$_] ||= 1 for 2, 3; # optional end
54 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
55 splice @cmds, $cmdarg, 2, (
56 #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
57 #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
58 -set => 'option:distort:viewport' => sprintf(
59 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
60 "w*$dim[4]", "h*$dim[5]", # width x height
61 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
62 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
63 "w*$dim[0]", "h*$dim[1]", # x+y offset
65 -distort => SRT => 0, # noop transform to apply viewport
71 -delete => '1--1', -background => 'white',
72 '-strip', -quality => '60%', -interlace => 'plane',
73 -gravity => defined $cmds ? 'northwest' : 'center',
75 $xyres ? (-resize => "$xyres^", -extent => $xyres) : (),
79 $imgpath->runcommand(@cmds);
82 sub runcommand ($, @cmds) {
85 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
86 "Failed to convert source image.",
88 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),