word/edit: decrease webp image quality to 40%
[sheet.git] / Shiar_Sheet / ImagePrep.pm
1 package Shiar_Sheet::ImagePrep;
2
3 use 5.014;
4 use warnings;
5
6 our $VERSION = '1.02';
7
8 sub new {
9         my ($class, $target) = @_;
10         bless \$target, $class;
11 }
12
13 sub download {
14         # copy changed remote url to local file
15         my $target = shift;
16         unlink $$target if -e $$target;
17         my $download = shift or return 1;
18         require LWP::UserAgent;
19         my $ua = LWP::UserAgent->new;
20         $ua->agent('/');
21         my $status = $ua->mirror($download, $$target);
22         $status->is_success
23                 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
24 }
25
26 sub dimensions {
27         my ($imgpath) = @_;
28         require IPC::Run;
29         IPC::Run::run(
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;
34 }
35
36 sub generate {
37         my ($imgpath, $thumbpath, $cmds) = @_;
38         if (not -e $$imgpath) {
39                 return !-e $thumbpath || unlink $thumbpath;
40         }
41         $cmds //= [];
42         $imgpath->convert($thumbpath, $cmds, '300x200') and # low-res cover
43         $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r,
44                 [@{$cmds}, -quality => 40], '600x400' # higher dpi tradeoff
45         );
46 }
47
48 sub convert {
49         my ($imgpath, $thumbpath, $cmds, $xyres) = @_;
50         #my ($w, $h) = $imgpath->dimensions;
51         #my $aspect = 3/2; # $xyres
52         my @cmds = @{$cmds};
53         if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
54                 # replace option by permillage crop
55                 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
56                 $dim[$_] ||= 1 for 2, 3; # optional end
57                 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
58                 splice @cmds, $cmdarg, 2, (
59                         #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
60                         #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
61                         -set => 'option:distort:viewport' => sprintf(
62                                 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
63                                 "w*$dim[4]", "h*$dim[5]", # width x height
64                                 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
65                                 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
66                                 "w*$dim[0]", "h*$dim[1]", # x+y offset
67                         ),
68                         -distort => SRT => 0, # noop transform to apply viewport
69                 );
70         }
71         @cmds = (
72                 'convert',
73                 $$imgpath,
74                 -delete => '1--1', -background => 'white',
75                 '-strip', -quality => '60%', -interlace => 'plane',
76                 -gravity => defined $cmds ? 'northwest' : 'center',
77                 @cmds,
78                 -resize => "$xyres^", -extent => $xyres,
79                 $thumbpath
80         );
81
82         require IPC::Run;
83         my $output;
84         IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
85                 "Failed to convert source image.",
86                 "@cmds\n" .
87                 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),
88         ];
89 }
90
91 1;