lib/App/Dapper/Images.pm

filter: Statementstmt 46.5% 108 / 232 Branchbran 45.8% 67 / 146 Conditioncond 52.9% 36 / 68 MC/DCmcdc 52.5% 21 / 40 Subroutinesub 77.2% 17 / 22 Podpod 80.0% 8 / 10 totaltotal 49.6% 257 / 518 CC 102 SCAR 48.0
CC
102
Cov
47%
image_tag
420.0
variants
272.0
command
90.0
CRAP 120.9
« lib/App/Dapper.pm ↑ Summary lib/App/Dapper/MCP.pm »
1 package App::Dapper::Images;
2  
3 =head1 NAME
4  
5 App::Dapper::Images - Image sizes, responsive images and lazy loading.
6  
7 =head1 DESCRIPTION
8  
9 With C<images> set in C<_config.yml>, Dapper improves the images in every
10 page it writes, after the site is built:
11  
12 images: true
13  
14 =over 4
15  
16 =item * B<Dimensions.> Each C<< <img> >> of a PNG, JPEG, GIF or WebP file in the
17 site gets C<width> and C<height>, so the page doesn't jump about as images
18 load.
19  
20 =item * B<Lazy loading.> Images after the first on a page get
21 C<loading="lazy">, so the browser fetches them only as they scroll into view.
22  
23 =item * B<Responsive sizes.> JPEG and PNG images wider than a size in C<sizes>
24 are resized to each smaller size (C<photo-480w.jpg>, C<photo-960w.jpg>, ...),
25 and the C<< <img> >> gets a C<srcset> listing them, so phones download small
26 files.
27  
28 =item * B<WebP.> With C<webp> on, a WebP copy of each size is made too, and
29 the image is wrapped in a C<< <picture> >> that offers it to browsers.
30  
31 =back
32  
33 Every part can be set:
34  
35 images:
36 dimensions: true # width and height (default: true)
37 lazy: true # loading="lazy" after the first image (default: true)
38 sizes: [480, 960, 1600] # widths to resize to (default; [] turns it off)
39 webp: true # WebP copies (default: true)
40 quality: 82 # JPEG and WebP quality (default: 82)
41 command: magick # magick, convert or vips (default: whichever is installed)
42  
43 Resizing needs ImageMagick (C<magick> or C<convert>) or libvips (C<vips>).
44 Without one, Dapper says so once and does everything else. Resized images are
45 kept in C<.dapper/images>, so each is only made once. An image with a
46 C<srcset> of its own, or inside a C<< <picture> >>, is left alone.
47  
48 =cut
49  
50 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 190 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 190 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 190 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 190 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 190 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 190 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 190 my @DEFAULT_SIZES = (480, 960, 1600);
64  
65 =head2 settings
66  
67 Takes the site and returns the image settings as a hash reference, or nothing
68 if C<images> is not set.
69  
70 =cut
71  
72 69 ▶sub settings {
Subroutine settings: called CC=10 SCAR=23.0
Pod documented
73 69 my ($site) = @_;
74  
75 69 my $value = $site->{images};
76 69 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 64 69
77 64 ▶ return if !ref $value and $value =~ /^(?:0|false|no|off|)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and $value =~ /^(?:0|false|no|off|)$/i 8 56 64
Condition100%3/3
not ref $value and $value =~ /^(?:0|false|no|off|)$/i
!ll&&!rl&&r
5158
MC/DC100%2/2
not ref $value and $value =~ /^(?:0|false|no|off|)$/i
not ref $value $value =~ /^(?:0|false|no|off|)$/i
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /^(?:0|false|no|off|)$/i
ABresult
0X0
100
111
78  
79 56 ▶ my %given = ref $value eq 'HASH' ? %$value : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 50 6 56
80 168 ▶ my $on = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
81 168 my ($name) = @_;
82 168 ▶ return 1 unless defined $given{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given{$name} 135 33 168
83 33 ▶ return $given{$name} =~ /^(?:0|false|no|off|)$/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$given{$name} =~ /^(?:0|false|no|off|)$/i ? : 18 15 33
84 56 };
85  
86 my @sizes = exists $given{sizes}
87 4 ? (ref $given{sizes} eq 'ARRAY' ? @{$given{sizes}} : grep { length } split /[\s,]+/, $given{sizes})
88 56 ▶ : @DEFAULT_SIZES;
Branch: 2 branches
BranchTrueFalseTotal
exists $given{'sizes'} ? : 8 48 56
ref $given{'sizes'} eq 'ARRAY' ? : 4 4 8
89 56 ▶ @sizes = sort { $a <=> $b } grep { /^\d+$/ and $_ > 0 } @sizes;
Condition100%3/3
/^\d+$/ && $_ > 0
!ll&&!rl&&r
41155
MC/DC100%2/2
/^\d+$/ && $_ > 0
/^\d+$/ $_ > 0
Truth table100%3/3
A && B
A= /^\d+$/B= $_ > 0
ABresult
0X0
100
111
90  
91 return {
92 dimensions => $on->('dimensions'),
93 lazy => $on->('lazy'),
94 webp => $on->('webp'),
95 sizes => \@sizes,
96 quality => defined $given{quality} && $given{quality} =~ /^\d+$/ ? $given{quality} : 82,
97 command => $given{command},
98 56 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $given{'quality'} && $given{'quality'} =~ /^\d+$/ ? : 1 55 56
Condition100%3/3
defined $given{"quality"} and $given{"quality"} =~ /^\d+$/
!ll&&!rl&&r
5231
MC/DC100%2/2
defined $given{"quality"} and $given{"quality"} =~ /^\d+$/
defined $given{"quality"} $given{"quality"} =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $given{"quality"}B= $given{"quality"} =~ /^\d+$/
ABresult
0X0
100
111
99 }
100  
101 =head2 process
102  
103 Takes a Dapper object whose site has just been written, and improves the
104 images in each HTML file it wrote.
105  
106 =cut
107  
108 3 ▶sub process {
Subroutine process: called CC=9 SCAR=43.0
Pod documented
109 3 my ($dapper) = @_;
110  
111 3 ▶ my $settings = settings($dapper->{site}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $settings = settings($dapper->{"site"}) or return 0 3 3
112 0 my $output = $dapper->{output};
113 0 ▶ my $base = defined $dapper->{site}->{url} ? $dapper->{site}->{url} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $dapper->{'site'}{'url'} ? : 0 0 0
114 0 $base =~ s{/+$}{};
115  
116 0 my $state = { settings => $settings, dapper => $dapper, made => {} };
117  
118 0 for my $relative (sort keys %{$dapper->{written}}) {
119 0 ▶ next unless $relative =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $relative =~ /\.html?$/i 0 0 0
120 0 my $file = catfile($output, $relative);
121 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
122  
123 0 my $html = App::Dapper::Utils::read_file($file);
124 0 my $page_dir = '/' . $relative;
125 0 $page_dir =~ s{[^/]*$}{};
126  
127 0 my $count = 0;
128 0 my $changed = 0;
129  
130 # Pictures and code are left as they are
131 0 my @kept;
132 0 $html =~ s{(<picture\b.*?</picture>|<pre\b.*?</pre>|<code\b.*?</code>|<!--.*?-->)}{push @kept, $1; "\x{0}DAPPERKEPT$#kept\x{0}"}gise;
133  
134 0 $html =~ s{(<img\b([^>]*?)\s*/?>)}{
135 0 my ($whole, $attributes) = ($1, $2);
136 0 my $tag = image_tag($state, $attributes, $page_dir, $base, $count++ == 0);
137 0 ▶ $changed = 1 if defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 0 0 0
138 0 ▶ defined $tag ? $tag : $whole;
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 0 0 0
139 }gise;
140  
141 0 $html =~ s{\x{0}DAPPERKEPT(\d+)\x{0}}{$kept[$1]}g;
142 0 ▶ App::Dapper::Utils::write_file($file, $html) if $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed 0 0 0
143 }
144 }
145  
146 =head2 image_tag
147  
148 Takes the processing state, the attributes of an C<< <img> >>, the directory
149 of the page it is on, the site's address and whether it is the first image
150 on the page. Returns the new HTML for it, or nothing to leave it alone.
151  
152 =cut
153  
154 0 ▶sub image_tag {
Subroutine image_tag: not called CC=20 SCAR=60.4
Pod documented
155 0 my ($state, $attributes, $page_dir, $base, $first) = @_;
156  
157 0 my $settings = $state->{settings};
158 0 my $output = $state->{dapper}->{output};
159  
160 0 my ($quote, $src) = $attributes =~ /\bsrc\s*=\s*(["'])(.*?)\1/is;
161 0 ▶ return unless defined $src;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src 0 0 0
162  
163 0 require App::Dapper::Check;
164 0 my $path = App::Dapper::Check::target_path($src, $page_dir, $base);
165 0 ▶ return unless defined $path and $path =~ /\.(?:png|jpe?g|gif|webp)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path =~ /\.(?:png|jpe?g|gif|webp)$/i 0 0 0
Condition0%0/3
defined $path and $path =~ /\.(?:png|jpe?g|gif|webp)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and $path =~ /\.(?:png|jpe?g|gif|webp)$/i
defined $path $path =~ /\.(?:png|jpe?g|gif|webp)$/i
Truth table0%0/3
A and B
A= defined $pathB= $path =~ /\.(?:png|jpe?g|gif|webp)$/i
ABresult
0X0
100
111
166 0 my $file = File::Spec->canonpath(catfile($output, $path));
167 0 ▶ return unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
168  
169 0 my ($width, $height) = image_size($file);
170 0 ▶ return unless $width and $height;
Branch: 1 branch
BranchTrueFalseTotal
unless $width and $height 0 0 0
Condition0%0/3
$width and $height
!ll&&!rl&&r
000
MC/DC0%0/2
$width and $height
$width $height
Truth table0%0/3
A and B
A= $widthB= $height
ABresult
0X0
100
111
171  
172 0 my $new = $attributes;
173 0 my $changed = 0;
174  
175 0 ▶ if ($settings->{dimensions} and $new !~ /\b(?:width|height)\s*=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"dimensions"} and not $new =~ /\b(?:width|height)\s*=/i) 0 0 0
Condition0%0/3
$settings->{"dimensions"} and not $new =~ /\b(?:width|height)\s*=/i
!ll&&!rl&&r
000
MC/DC0%0/2
$settings->{"dimensions"} and not $new =~ /\b(?:width|height)\s*=/i
$settings->{"dimensions"} not $new =~ /\b(?:width|height)\s*=/i
Truth table0%0/3
A and not(B)
A= $settings->{"dimensions"}B= not $new =~ /\b(?:width|height)\s*=/i
ABresult
0X0
100
111
176 0 $new .= qq{ width="$width" height="$height"};
177 0 $changed = 1;
178 }
179 0 ▶ if ($settings->{lazy} and not $first and $new !~ /\bloading\s*=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"lazy"} and not $first and not $new =~ /\bloading\s*=/i) 0 0 0
Condition0%0/6
$settings->{"lazy"} and not $first and not $new =~ /\bloading\s*=/i
!ll&&!rl&&r
000
$settings->{"lazy"} and not $first
!ll&&!rl&&r
000
MC/DC0%0/3
$settings->{"lazy"} and not $first and not $new =~ /\bloading\s*=/i
$settings->{"lazy"} not $first not $new =~ /\bloading\s*=/i
Truth table0%0/4
A and not(B) and not(C)
A= $settings->{"lazy"}B= not $firstC= not $new =~ /\bloading\s*=/i
ABCresult
0XX0
10X0
1100
1111
180 0 $new .= q{ loading="lazy" decoding="async"};
181 0 $changed = 1;
182 }
183  
184 0 my $picture;
185 0 ▶ if (@{$settings->{sizes}} and $path =~ /\.(?:png|jpe?g)$/i and $new !~ /\bsrcset\s*=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$settings->{"sizes"};} and $path =~ /\.(?:png|jpe?g)$/i and not $new =~ /\bsrcset\s*=/i) 0 0 0
Condition0%0/6
@{$settings->{"sizes"};} and $path =~ /\.(?:png|jpe?g)$/i and not $new =~ /\bsrcset\s*=/i
!ll&&!rl&&r
000
@{$settings->{"sizes"};} and $path =~ /\.(?:png|jpe?g)$/i
!ll&&!rl&&r
000
MC/DC0%0/3
@{$settings->{"sizes"};} and $path =~ /\.(?:png|jpe?g)$/i and not $new =~ /\bsrcset\s*=/i
@{$settings->{"sizes"};} $path =~ /\.(?:png|jpe?g)$/i not $new =~ /\bsrcset\s*=/i
Truth table0%0/4
A and B and not(C)
A= @{$settings->{"sizes"};}B= $path =~ /\.(?:png|jpe?g)$/iC= not $new =~ /\bsrcset\s*=/i
ABCresult
0XX0
10X0
1100
1111
186 0 my @widths = grep { $_ < $width } @{$settings->{sizes}};
187 0 ▶ if (@widths and my @made = variants($state, $file, $width, $height, \@widths)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@widths and my(@made) = variants($state, $file, $width, $height, \@widths)) 0 0 0
Condition0%0/3
@widths and my(@made) = variants($state, $file, $width, $height, \@widths)
!ll&&!rl&&r
000
MC/DC0%0/2
@widths and my(@made) = variants($state, $file, $width, $height, \@widths)
@widths my(@made) = variants($state, $file, $width, $height, \@widths)
Truth table0%0/3
A and B
A= @widthsB= my(@made) = variants($state, $file, $width, $height, \@widths)
ABresult
0X0
100
111
188 0 ▶ my $srcset = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
189 0 my ($ext) = @_;
190 0 ▶ my @list = map { variant_url($src, $_->{full} ? undef : $_->{width}, $ext) . " $_->{width}w" }
Branch: 1 branch
BranchTrueFalseTotal
$_->{'full'} ? : 0 0 0
191 0 ▶ grep { $_->{ext} eq ($ext || 'same') } @made;
Condition0%0/2
$ext || 'same'
l!l
00
MC/DC0%0/1
$ext || 'same'
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
192 0 return join(', ', @list);
193 0 };
194 0 my $sizes = qq{(max-width: ${width}px) 100vw, ${width}px};
195 0 $new .= qq{ srcset="} . $srcset->() . ", $src ${width}w" . qq{" sizes="$sizes"};
196 0 ▶ if (grep { $_->{ext} eq 'webp' } @made) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"ext"} eq "webp";} @made) 0 0 0
197 0 $picture = qq{<source type="image/webp" srcset="} . $srcset->('webp') . qq{" sizes="$sizes">};
198 }
199 0 $changed = 1;
200 }
201 }
202  
203 0 ▶ return unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 0 0 0
204 0 my $img = "<img$new>";
205 0 ▶ return defined $picture ? "<picture>$picture$img</picture>" : $img;
Branch: 1 branch
BranchTrueFalseTotal
defined $picture ? : 0 0 0
206 }
207  
208 =head2 variant_url
209  
210 Takes an image URL, a width and (optionally) a new extension, and returns the
211 URL of the resized copy: C<photo.jpg> at 480 is C<photo-480w.jpg>, and as WebP
212 C<photo-480w.webp>. Without a width, the full-size copy: C<photo.webp>.
213  
214 =cut
215  
216 4 ▶sub variant_url {
Subroutine variant_url: called CC=5 SCAR=16.3
Pod documented
217 4 my ($url, $width, $ext) = @_;
218  
219 4 ▶ my ($stem, $old, $rest) = $url =~ /^(.*)\.([A-Za-z0-9]+)((?:[?#].*)?)$/s
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ /^(.*)\.([A-Za-z0-9]+)((?:[?#].*)?)$/s or return $url 4 0 4
220 or return $url;
221 4 ▶ $ext = $old unless defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 2 2 4
222 4 ▶ my $size = defined $width ? "-${width}w" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $width ? : 3 1 4
223 4 ▶ return "$stem$size.$ext" . (defined $rest ? $rest : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $rest ? : 4 0 4
224 }
225  
226 =head2 variants
227  
228 Takes the processing state, an image in the output directory, its width and
229 height, and the widths wanted. Makes each resized copy (and its WebP copy, if
230 WebP is on) next to the image, and returns a list of hash references with the
231 C<width> and C<ext> (C<same> or C<webp>) of each copy made. Returns nothing if
232 no resizing command is installed.
233  
234 =cut
235  
236 0 ▶sub variants {
Subroutine variants: not called CC=16 SCAR=56.1
Pod documented
237 0 my ($state, $file, $width, $height, $widths) = @_;
238  
239 0 my $settings = $state->{settings};
240 0 my $dapper = $state->{dapper};
241 0 ▶ my $command = command($state) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $command = command($state) or return 0 0 0
242  
243 0 my $digest = do {
244 0 ▶ open(my $fh, '<:raw', $file) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return 0 0 0
245 0 Digest::MD5->new->addfile($fh)->hexdigest;
246 };
247  
248 0 my $cache = catfile('.dapper', 'images');
249 0 ▶ File::Path::make_path($cache) unless -d $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $cache 0 0 0
250  
251 0 my @made;
252 0 my @formats = ('same');
253 0 ▶ push @formats, 'webp' if $settings->{webp};
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"webp"} 0 0 0
254  
255 0 (my $ext = $file) =~ s/.*\.//;
256 0 for my $format (@formats) {
257 0 ▶ my $out_ext = $format eq 'webp' ? 'webp' : $ext;
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'webp' ? : 0 0 0
258 0 my @sizes = @$widths;
259 0 ▶ push @sizes, $width if $format eq 'webp';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "webp" 0 0 0
260 0 for my $size (@sizes) {
261 0 my $cached = catfile($cache, "$digest-$size-$settings->{quality}.$out_ext");
262 0 ▶ if (not -s $cached) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $cached) 0 0 0
263 0 my $ok = resize($command, $file, $cached, $size, $settings->{quality});
264 0 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
265 0 unlink $cached;
266 0 $dapper->problem('warning', $file, "could not resize $file with $command->[0]");
267 0 next;
268 }
269 }
270 0 ▶ my $target = $size == $width ? variant_url($file, undef, $out_ext) : variant_url($file, $size, $out_ext);
Branch: 1 branch
BranchTrueFalseTotal
$size == $width ? : 0 0 0
271 0 ▶ File::Copy::copy($cached, $target) or next;
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($cached, $target) or next 0 0 0
272 0 $dapper->record_written($target);
273 0 ▶ push @made, { width => $size, ext => $format } unless $size == $width;
Branch: 1 branch
BranchTrueFalseTotal
unless $size == $width 0 0 0
274 }
275 }
276  
277 # The full-size WebP copy is listed with the other sizes
278 0 ▶ if ($settings->{webp} and grep { $_->{ext} eq 'webp' } @made) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"webp"} and grep {$_->{"ext"} eq "webp";} @made) 0 0 0
Condition0%0/3
$settings->{"webp"} and grep {$_->{"ext"} eq "webp";} @made
!ll&&!rl&&r
000
MC/DC0%0/2
$settings->{"webp"} and grep {$_->{"ext"} eq "webp";} @made
$settings->{"webp"} grep {$_->{"ext"} eq "webp";} @made
Truth table0%0/3
A and B
A= $settings->{"webp"}B= grep {$_->{"ext"} eq "webp";} @made
ABresult
0X0
100
111
279 0 push @made, { width => $width, ext => 'webp', full => 1 };
280 }
281  
282 0 return @made;
283 }
284  
285 =head2 command
286  
287 Returns the command used to resize images (as an array reference), finding it
288 the first time: the C<command> setting, or C<magick>, C<convert> or C<vips>,
289 whichever is installed. Returns nothing, after one warning, if there is none.
290  
291 =cut
292  
293 0 ▶sub command {
Subroutine command: not called CC=9 SCAR=45.0
Pod documented
294 0 my ($state) = @_;
295  
296 0 ▶ return $state->{command} if exists $state->{command};
Branch: 1 branch
BranchTrueFalseTotal
if exists $state->{"command"} 0 0 0
297  
298 0 ▶ my @wanted = defined $state->{settings}->{command} ? ($state->{settings}->{command}) : qw/ magick convert vips /;
Branch: 1 branch
BranchTrueFalseTotal
defined $state->{'settings'}{'command'} ? : 0 0 0
299 0 for my $name (@wanted) {
300 my $found = $name =~ m{/} ? (-x $name ? $name : undef)
301 0 ▶ : (grep { -x catfile($_, $name) } File::Spec->path()) ? $name : undef;
Branch: 3 branches
BranchTrueFalseTotal
$name =~ m[/] ? : 0 0 0
-x $name ? : 0 0 0
grep({-x catfile($_, $name);} 'File::Spec'->path) ? : 0 0 0
302 0 ▶ next unless defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 0 0 0
303 0 my ($kind) = $name =~ m{(magick|convert|vips)[^/]*$};
304 0 ▶ $kind = 'magick' unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 0 0
305 0 return $state->{command} = [ $found, $kind ];
306 }
307  
308 $state->{dapper}->problem('warning', $state->{dapper}->{config},
309 0 'resizing images needs ImageMagick or libvips; install one (for example "brew install imagemagick"), or set sizes: [] under images');
310 0 return $state->{command} = undef;
311 }
312  
313 =head2 resize
314  
315 Takes the command (from L</command>), an image, the file to write, the width
316 and the quality, and resizes the image. Returns true if it worked.
317  
318 =cut
319  
320 0 ▶sub resize {
Subroutine resize: not called CC=3 SCAR=24.8
Pod documented
321 0 my ($command, $from, $to, $width, $quality) = @_;
322  
323 0 my ($program, $kind) = @$command;
324 0 ▶ my @run = $kind eq 'vips'
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'vips' ? : 0 0 0
325 ? ($program, 'thumbnail', $from, "${to}[Q=$quality,strip]", $width)
326 : ($program, $from, '-auto-orient', '-strip', '-resize', "${width}x", '-quality', $quality, $to);
327  
328 # Keep the programs quiet; their errors are reported as a warning instead
329 0 open(my $null, '>', File::Spec->devnull);
330 0 open(my $saved_out, '>&', \*STDOUT);
331 0 open(my $saved_err, '>&', \*STDERR);
332 0 open(STDOUT, '>&', $null);
333 0 open(STDERR, '>&', $null);
334 0 my $status = system(@run);
335 0 open(STDOUT, '>&', $saved_out);
336 0 open(STDERR, '>&', $saved_err);
337  
338 0 ▶ return $status == 0 && -s $to;
Condition0%0/3
$status == 0 && -s $to
!ll&&!rl&&r
000
MC/DC0%0/2
$status == 0 && -s $to
$status == 0 -s $to
Truth table0%0/3
A && B
A= $status == 0B= -s $to
ABresult
0X0
100
111
339 }
340  
341 =head2 image_size
342  
343 Takes an image file (PNG, GIF, JPEG or WebP) and returns its width and height
344 as it is displayed (so a JPEG turned by its EXIF orientation reports its
345 turned size), or nothing if it can't tell.
346  
347 =cut
348  
349 92 ▶sub image_size {
Subroutine image_size: called CC=12 SCAR=24.8
Pod documented
350 92 my ($file) = @_;
351  
352 92 ▶ open(my $fh, '<:raw', $file) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return 91 1 92
353 91 my $data = '';
354 91 read($fh, $data, 256 * 1024);
355 91 close $fh;
356  
357 91 ▶ if ($data =~ /^\x89PNG\r\n\x1a\n/ and length $data >= 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\x89PNG\r\n\x1a\n/ and length $data >= 24) 5 86 91
Condition100%3/3
$data =~ /^\x89PNG\r\n\x1a\n/ and length $data >= 24
!ll&&!rl&&r
8425
MC/DC100%2/2
$data =~ /^\x89PNG\r\n\x1a\n/ and length $data >= 24
$data =~ /^\x89PNG\r\n\x1a\n/ length $data >= 24
Truth table100%3/3
A and B
A= $data =~ /^\x89PNG\r\n\x1a\n/B= length $data >= 24
ABresult
0X0
100
111
358 5 return unpack('NN', substr($data, 16, 8));
359 }
360 86 ▶ if ($data =~ /^GIF8[79]a/ and length $data >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^GIF8[79]a/ and length $data >= 10) 5 81 86
Condition100%3/3
$data =~ /^GIF8[79]a/ and length $data >= 10
!ll&&!rl&&r
8015
MC/DC100%2/2
$data =~ /^GIF8[79]a/ and length $data >= 10
$data =~ /^GIF8[79]a/ length $data >= 10
Truth table100%3/3
A and B
A= $data =~ /^GIF8[79]a/B= length $data >= 10
ABresult
0X0
100
111
361 5 return unpack('vv', substr($data, 6, 4));
362 }
363 81 ▶ if ($data =~ /^RIFF....WEBP/s and length $data >= 30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^RIFF....WEBP/s and length $data >= 30) 11 70 81
Condition100%3/3
$data =~ /^RIFF....WEBP/s and length $data >= 30
!ll&&!rl&&r
69111
MC/DC100%2/2
$data =~ /^RIFF....WEBP/s and length $data >= 30
$data =~ /^RIFF....WEBP/s length $data >= 30
Truth table100%3/3
A and B
A= $data =~ /^RIFF....WEBP/sB= length $data >= 30
ABresult
0X0
100
111
364 11 my $chunk = substr($data, 12, 4);
365 11 ▶ if ($chunk eq 'VP8 ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk eq "VP8 ") 3 8 11
366 3 my ($w, $h) = unpack('vv', substr($data, 26, 4));
367 3 return ($w & 0x3fff, $h & 0x3fff);
368 }
369 8 ▶ if ($chunk eq 'VP8L') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk eq "VP8L") 3 5 8
370 3 my $bits = unpack('V', substr($data, 21, 4));
371 3 return (1 + ($bits & 0x3fff), 1 + (($bits >> 14) & 0x3fff));
372 }
373 5 ▶ if ($chunk eq 'VP8X') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk eq "VP8X") 4 1 5
374 4 my @b = unpack('C6', substr($data, 24, 6));
375 4 return (1 + ($b[0] | $b[1] << 8 | $b[2] << 16), 1 + ($b[3] | $b[4] << 8 | $b[5] << 16));
376 }
377 1 return;
378 }
379 70 ▶ return jpeg_size($data) if $data =~ /^\xff\xd8/;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^\xff\xd8/ 57 13 70
380 13 return;
381 }
382  
383 57 ▶sub jpeg_size {
Subroutine jpeg_size: called CC=15 SCAR=27.1
Pod undocumented
384 57 my ($data) = @_;
385  
386 57 my ($pos, $orientation) = (2, 1);
387 57 ▶ while ($pos + 4 <= length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 4 <= length $data) 138 3 141
388 138 my ($marker, $type) = unpack('CC', substr($data, $pos, 2));
389 138 ▶ return unless $marker == 0xff;
Branch: 1 branch
BranchTrueFalseTotal
unless $marker == 255 1 137 138
390 137 ▶ if ($type == 0xff) { $pos++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 255) 2 135 137
391 135 my $length = unpack('n', substr($data, $pos + 2, 2));
392  
393 135 ▶ if ($type == 0xe1 and substr($data, $pos + 4, 6) eq "Exif\0\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 225 and substr($data, $pos + 4, 6) eq "Exif\0\0") 38 97 135
Condition100%3/3
$type == 225 and substr($data, $pos + 4, 6) eq "Exif\0\0"
!ll&&!rl&&r
96138
MC/DC100%2/2
$type == 225 and substr($data, $pos + 4, 6) eq "Exif\0\0"
$type == 225 substr($data, $pos + 4, 6) eq "Exif\0\0"
Truth table100%3/3
A and B
A= $type == 225B= substr($data, $pos + 4, 6) eq "Exif\0\0"
ABresult
0X0
100
111
394 38 my $found = exif_orientation(substr($data, $pos + 10, $length - 8));
395 38 ▶ $orientation = $found if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 28 10 38
396 }
397 135 ▶ if ($type >= 0xc0 and $type <= 0xcf and $type != 0xc4 and $type != 0xc8 and $type != 0xcc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type >= 192 and $type <= 207 and $type != 196 and $type != 200 and $type != 204) 53 82 135
Condition100%12/12
$type >= 192 and $type <= 207 and $type != 196 and $type != 200 and $type != 204
!ll&&!rl&&r
81153
$type >= 192 and $type <= 207 and $type != 196 and $type != 200
!ll&&!rl&&r
80154
$type >= 192 and $type <= 207 and $type != 196
!ll&&!rl&&r
79155
$type >= 192 and $type <= 207
!ll&&!rl&&r
27756
MC/DC100%5/5
$type >= 192 and $type <= 207 and $type != 196 and $type != 200 and $type != 204
$type >= 192 $type <= 207 $type != 196 $type != 200 $type != 204
Truth table100%6/6
A and B and C and D and E
A= $type >= 192B= $type <= 207C= $type != 196D= $type != 200E= $type != 204
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
398 53 ▶ return if $pos + 9 > length $data;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + 9 > length $data 1 52 53
399 52 my ($height, $width) = unpack('nn', substr($data, $pos + 5, 4));
400 52 ▶ return $orientation >= 5 && $orientation <= 8 ? ($height, $width) : ($width, $height);
Branch: 1 branch
BranchTrueFalseTotal
$orientation >= 5 && $orientation <= 8 ? : 15 37 52
Condition100%3/3
$orientation >= 5 and $orientation <= 8
!ll&&!rl&&r
35215
MC/DC100%2/2
$orientation >= 5 and $orientation <= 8
$orientation >= 5 $orientation <= 8
Truth table100%3/3
A and B
A= $orientation >= 5B= $orientation <= 8
ABresult
0X0
100
111
401 }
402 82 $pos += 2 + $length;
403 }
404 3 return;
405 }
406  
407 38 ▶sub exif_orientation {
Subroutine exif_orientation: called CC=8 SCAR=20.8
Pod undocumented
408 38 my ($tiff) = @_;
409  
410 38 ▶ return unless length $tiff >= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless length $tiff >= 8 1 37 38
411 37 my $order = substr($tiff, 0, 2);
412 37 ▶ my ($short, $long) = $order eq 'II' ? ('v', 'V') : $order eq 'MM' ? ('n', 'N') : return;
Branch: 2 branches
BranchTrueFalseTotal
$order eq 'II' ? : 17 20 37
$order eq 'MM' ? : 19 1 20
413 36 my $ifd = unpack($long, substr($tiff, 4, 4));
414 36 ▶ return unless $ifd + 2 <= length $tiff;
Branch: 1 branch
BranchTrueFalseTotal
unless $ifd + 2 <= length $tiff 2 34 36
415 34 my $entries = unpack($short, substr($tiff, $ifd, 2));
416 34 for my $i (0 .. $entries - 1) {
417 40 my $entry = $ifd + 2 + $i * 12;
418 40 ▶ last if $entry + 12 > length $tiff;
Branch: 1 branch
BranchTrueFalseTotal
if $entry + 12 > length $tiff 2 38 40
419 38 ▶ next unless unpack($short, substr($tiff, $entry, 2)) == 0x0112;
Branch: 1 branch
BranchTrueFalseTotal
unless unpack($short, substr($tiff, $entry, 2)) == 274 8 30 38
420 30 return unpack($short, substr($tiff, $entry + 8, 2));
421 }
422 4 return;
423 }
424  
425 190 1;
« lib/App/Dapper.pm ↑ Summary lib/App/Dapper/MCP.pm »