blib/lib/App/ImageMagickUtils.pm

filter: Statementstmt 20.0% 40 / 200 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 33.3% 6 / 18 Podpod 100% 6 / 6 totaltotal 16.1% 52 / 323 CC 47 SCAR 58.0
CC
47
Cov
14%
downsize_image
652.2
resize_image
240.0
convert_image_to
72.0
CRAP 330.6
↑ Summary
1 package App::ImageMagickUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Perinci::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2026-09-16'; # DATE
12 1 our $DIST = 'App-ImageMagickUtils'; # DIST
13 1 our $VERSION = '0.025'; # VERSION
14  
15 1 our %SPEC;
16  
17 1 $SPEC{':package'} = {
18 v => 1.1,
19 summary => 'Utilities related to ImageMagick',
20 };
21  
22 1 our %argspec0plus_files = (
23 files => {
24 'x.name.is_plural' => 1,
25 'x.name.singular' => 'file',
26 schema => ['array*' => of => 'filename*'],
27 req => 1,
28 pos => 0,
29 slurpy => 1,
30 },
31 );
32  
33 1 our %argspec1plus_files = (
34 files => {
35 'x.name.is_plural' => 1,
36 'x.name.singular' => 'file',
37 schema => ['array*' => of => 'filename*'],
38 req => 1,
39 pos => 1,
40 slurpy => 1,
41 },
42 );
43  
44 1 our %argspec0plus_files__comp_png = (
45 files => {
46 'x.name.is_plural' => 1,
47 'x.name.singular' => 'file',
48 schema => ['array*' => of => 'filename*'],
49 'x.completion' => [filename => {file_ext_filter=>'png'}],
50 req => 1,
51 pos => 0,
52 slurpy => 1,
53 },
54 );
55  
56 1 our %argspec0plus_files__comp_jpg = (
57 files => {
58 'x.name.is_plural' => 1,
59 'x.name.singular' => 'file',
60 schema => ['array*' => of => 'filename*'],
61 'x.completion' => [filename => {file_ext_filter=>qr/^(jpe?g)$/i}],
62 req => 1,
63 pos => 0,
64 slurpy => 1,
65 },
66 );
67  
68 1 our %argspec0plus_files__comp_pdf = (
69 files => {
70 'x.name.is_plural' => 1,
71 'x.name.singular' => 'file',
72 schema => ['array*' => of => 'filename*'],
73 'x.completion' => [filename => {file_ext_filter=>'pdf'}],
74 req => 1,
75 pos => 0,
76 slurpy => 1,
77 },
78 );
79  
80 1 our %argspecs_delete = (
81 delete_original => {
82 summary => 'Delete (unlink) the original file after downsizing',
83 schema => 'bool*',
84 description => <<'MARKDOWN',
85  
86 See also the `trash_original` option.
87  
88 MARKDOWN
89 cmdline_aliases => {D=>{}},
90 },
91 trash_original => {
92 summary => 'Trash the original file after downsizing',
93 schema => 'bool*',
94 description => <<'MARKDOWN',
95  
96 This option uses the <pm:File::Trash::FreeDesktop> module to do the trashing.
97 Compared to deletion, with this option you can still restore the trashed
98 original files from the Trash directory.
99  
100 See also the `delete_original` option.
101  
102 MARKDOWN
103 cmdline_aliases => {T=>{}},
104 },
105 );
106  
107 1 our %argspecopt_quality = (
108 quality => {
109 summary => 'Quality setting (for JPEG/PNG), 1 (best compression, worst quality) to 100 (least compression, best quality)',
110 schema => ['int*', between=>[0,100]],
111 default => 92,
112 cmdline_aliases => {q=>{}},
113 },
114 );
115  
116 1 our %argspecopt_quality__def40 = (
117 quality => {
118 summary => 'Quality setting (for JPEG/PNG), 1 (best compression, worst quality) to 100 (least compression, best quality)',
119 schema => ['int*', between=>[0,100]],
120 default => 40,
121 cmdline_aliases => {q=>{}},
122 },
123 );
124  
125 1 our %argspecopt_overwrite = (
126 overwrite => {
127 summary => 'Whether to overwrite existing output files',
128 schema => 'bool',
129 cmdline_aliases => {O=>{}},
130 },
131 );
132  
133 1 our %args_rels = (
134 choose_one => [qw/delete_original trash_original/],
135 );
136  
137 0 ▶sub _nearest {
Subroutine _nearest: not called CC=1 SCAR=6.9
138 0 sprintf("%d", $_[0]/$_[1]) * $_[1];
139 }
140  
141 $SPEC{downsize_image} = {
142 v => 1.1,
143 summary => 'Reduce image size, by default via compressing to JPEG quality 40 and downsizing to 1024p',
144 description => <<'MARKDOWN',
145  
146 This utility uses <prog:convert> utility to compress an image into JPEG with
147 default quality of 40 and downsized to 1024p (shortest side to 1024px).
148  
149 Output filenames are:
150  
151 ORIGINAL_NAME.q40.jpg
152  
153 or (if downsizing is done):
154  
155 ORIGINAL_NAME.1024p-q40.jgp
156  
157 MARKDOWN
158 args => {
159 %argspec0plus_files,
160 %argspecopt_quality__def40,
161 downsize_to => {
162 schema => ['str*', in=>['', '640', '800', '1024', '1536', '2048']],
163 default => '1024',
164 description => <<'MARKDOWN',
165  
166 Downsizing will only be done if the input image's shortest side is indeed larger
167 then the target downsize.
168  
169 To disable downsizing, set `--downsize-to` to '' (empty string), or specify on
170 `--dont-downsize` on the CLI.
171  
172 MARKDOWN
173 cmdline_aliases => {
174 0 ▶ dont_downsize => {summary=>"Alias for --downsize-to ''", is_flag=>1, code=>sub {$_[0]{downsize_to} = ''}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 ▶ no_downsize => {summary=>"Alias for --downsize-to ''", is_flag=>1, code=>sub {$_[0]{downsize_to} = ''}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 ▶ S => {summary=>"Alias for --downsize-to ''", is_flag=>1, code=>sub {$_[0]{downsize_to} = ''}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 ▶ 1536 => {summary=>"Shortcut for --downsize-to=1536", is_flag=>1, code=>sub {$_[0]{downsize_to} = '1536'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 ▶ 2048 => {summary=>"Shortcut for --downsize-to=2048", is_flag=>1, code=>sub {$_[0]{downsize_to} = '2048'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 },
180 },
181 1 skip_whatsapp => {
182 summary => 'Skip WhatsApp images',
183 'summary.alt.bool.not' => 'Do not skip WhatsApp images',
184 schema => 'bool*',
185 default => 1,
186 description => <<'MARKDOWN',
187  
188 By default, assuming that WhatsApp already compresses images, when given a
189 filename that matches a WhatsApp image filename, e.g. `IMG-20220508-WA0001.jpg`
190 (will be checked using <pm:Regexp::Pattern::Filename::Type::Image::WhatsApp>),
191 will skip downsizing. The `--no-skip-whatsapp` option will process such
192 filenames nevertheless.
193  
194 MARKDOWN
195 },
196 skip_downsized => {
197 summary => 'Skip previously downsized images',
198 'summary.alt.bool.not' => 'Do not skip previously downsized images',
199 schema => 'bool*',
200 default => 1,
201 description => <<'MARKDOWN',
202  
203 By default, when given a filename that looks like it's already downsized, e.g.
204 `foo.1024-q40.jpg` or `foo.q40.jpg`, will skip downsizing. The
205 `--no-skip-downsized` option will process such filenames nevertheless.
206  
207 MARKDOWN
208 },
209 %argspecs_delete,
210 },
211 args_rels => \%args_rels,
212 features => {
213 dry_run => 1,
214 },
215 examples => [
216 {
217 summary => 'The default setting is to downsize to 1024p',
218 src => 'downsize-image *',
219 src_plang => 'bash',
220 test => 0,
221 'x.doc.show_result' => 0,
222 },
223 {
224 summary => 'Do not downsize, just recompress to JPEG quality 40, delete original files',
225 src => 'downsize-image --dont-downsize --delete-original *',
226 src_plang => 'bash',
227 test => 0,
228 'x.doc.show_result' => 0,
229 },
230 ],
231 };
232 0 ▶sub downsize_image {
Subroutine downsize_image: not called CC=26 SCAR=64.8
Pod documented
233 0 require File::Which;
234 0 require Image::Size;
235 0 require IPC::System::Options;
236 #require Filename::Type::Image;
237  
238 0 my %args = @_;
239  
240 0 my $convert_path = File::Which::which("convert");
241 0 my $downsize_to = $args{downsize_to};
242 0 ▶ my $skip_whatsapp = $args{skip_whatsapp} // 1;
Condition0%0/2
$args{"skip_whatsapp"} // 1
l!l
00
MC/DC0%0/1
$args{"skip_whatsapp"} // 1
$args{"skip_whatsapp"}
Truth table0%0/2
A
A= $args{"skip_whatsapp"}
Aresult
11
00
243 0 ▶ my $skip_downsized = $args{skip_downsized} // 1;
Condition0%0/2
$args{"skip_downsized"} // 1
l!l
00
MC/DC0%0/1
$args{"skip_downsized"} // 1
$args{"skip_downsized"}
Truth table0%0/2
A
A= $args{"skip_downsized"}
Aresult
11
00
244  
245 0 ▶ unless ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-dry_run"}) 0 0 0
246 0 ▶ return [400, "Cannot find convert in path"] unless defined $convert_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $convert_path 0 0 0
247 0 ▶ return [400, "convert path $convert_path is not executable"] unless -x $convert_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $convert_path 0 0 0
248 }
249  
250 0 my ($num_files, $num_success) = (0, 0);
251 0 my $trash;
252 FILE:
253 0 for my $file (@{$args{files}}) {
254 0 log_info "Processing file %s ...", $file;
255 0 $num_files++;
256  
257 0 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
258 0 log_error "No such file %s, skipped", $file;
259 0 next FILE;
260 }
261  
262 #my $res = Filename::Type::Image::check_image_filename(filename => $file);
263 0 my ($width, $height, $fmt) = Image::Size::imgsize($file);
264 0 ▶ unless ($width) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($width) 0 0 0
265 0 log_error "Filename '%s' is not image (%s), skipped", $file, $fmt;
266 0 next FILE;
267 }
268  
269 0 ▶ if ($skip_whatsapp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_whatsapp) 0 0 0
270 0 require Regexp::Pattern::Filename::Type::Image::WhatsApp;
271 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
272 0 ▶ if ($file =~ $Regexp::Pattern::Filename::Type::Image::WhatsApp::RE{filename_type_image_whatsapp}{pat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$Regexp::Pattern::Filename::Type::Image::WhatsApp::RE{"filename_type_image_whatsapp"}{"pat"}/) 0 0 0
273 0 log_info "Filename '%s' looks like a WhatsApp image, skip downsizing due to --skip-whatsapp option is in effect", $file;
274 0 next FILE;
275 }
276 }
277  
278 0 ▶ if ($skip_downsized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_downsized) 0 0 0
279 0 ▶ if ($file =~ /\.(?:\d+p?-)?q(?:\d{1,3})\.\w+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.(?:\d+p?-)?q(?:\d{1,3})\.\w+\z/) 0 0 0
280 0 log_info "Filename '%s' looks like it's already downsized, skip downsizing due to --skip-downsized option is in effect", $file;
281 0 next FILE;
282 }
283 }
284  
285 0 ▶ my $q = $args{quality} // 40;
Condition0%0/2
$args{"quality"} // 40
l!l
00
MC/DC0%0/1
$args{"quality"} // 40
$args{"quality"}
Truth table0%0/2
A
A= $args{"quality"}
Aresult
11
00
286 0 my @convert_args = (
287 $file,
288 );
289  
290 0 my $downsized;
291 #say "D:downsize_to=<$downsize_to>, width=<$width>, height=<$height>, q=<$q>";
292 DOWNSIZE: {
293 0 ▶ last unless $downsize_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $downsize_to 0 0 0
294 0 my $ratio;
295 0 ▶ my $shortest_side = $width > $height ? $height : $width;
Branch: 1 branch
BranchTrueFalseTotal
$width > $height ? : 0 0 0
296 0 ▶ last unless $shortest_side > $downsize_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $shortest_side > $downsize_to 0 0 0
297 0 $downsized++;
298 0 push @convert_args, "-resize", "$downsize_to^>";
299 } # DOWNSIZE
300  
301 0 push @convert_args, "-quality", $q;
302  
303 0 my $output_file = $file;
304 0 ▶ my $ext = $downsized ? ".$downsize_to-q$q.jpg" : ".q$q.jpg";
Branch: 1 branch
BranchTrueFalseTotal
$downsized ? : 0 0 0
305 0 ▶ $output_file =~ s/(\.\w{3,4})?\z/($1 eq ".jpg" ? "" : $1) . $ext/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '.jpg' ? : 0 0 0
306  
307 0 push @convert_args, (
308 $output_file,
309 );
310  
311 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
312 0 log_info "[DRY-RUN] Running $convert_path with args %s ...", \@convert_args;
313 0 $num_success++;
314 0 next FILE;
315 }
316  
317 0 log_info "Running $convert_path with args %s ...", \@convert_args;
318 0 IPC::System::Options::system(
319 $convert_path, @convert_args,
320 );
321 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) { } 0 0 0
322 0 ▶ my ($exit_code, $signal, $core_dump) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
323 0 log_error "convert for $file failed: exit_code=$exit_code, signal=$signal, core_dump=$core_dump";
324 } else {
325 0 ▶ if ($args{trash_original}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'trash_original'}) { } 0 0 0
elsif ($args{'delete_original'}) { } 0 0 0
326 0 require File::Trash::FreeDesktop;
327 0 ▶ $trash //= File::Trash::FreeDesktop->new;
Condition0%0/3
$trash //= "File::Trash::FreeDesktop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$trash //= "File::Trash::FreeDesktop"->new
$trash "File::Trash::FreeDesktop"->new
Truth table0%0/3
A //= B
A= $trashB= "File::Trash::FreeDesktop"->new
ABresult
1X1
011
000
328 0 log_info "Trashing original file %s ...", $file;
329 # will die upon failure, currently we don't trap
330 0 $trash->trash($file);
331 } elsif ($args{delete_original}) {
332 # currently we ignore the results
333 0 log_info "Deleting original file %s ...", $file;
334 0 unlink $file;
335 }
336 0 $num_success++;
337 }
338 }
339  
340 0 ▶ $num_success == 0 ? [500, "All files failed"] : [200];
Branch: 1 branch
BranchTrueFalseTotal
$num_success == 0 ? : 0 0 0
341 }
342  
343 $SPEC{resize_image} = {
344 1 v => 1.1,
345 summary => 'Resize images',
346 description => <<'MARKDOWN',
347  
348 This utility is a shortcut for <prog:magick> utility as follow:
349  
350 % magick INPUT.PNG -resize SIZE_NOTATION OUTPUT.newxsize.PNG
351  
352 It offers convenience: automatic output file naming (specify new dimension in
353 filename), non-overwrite default with overwrite option, support multiple files.
354  
355 MARKDOWN
356 args => {
357 size_notation => {
358 schema => 'str*',
359 req => 1,
360 cmdline_aliases => {s=>{}},
361 },
362 %argspec0plus_files,
363 %argspecs_delete,
364 %argspecopt_overwrite,
365 },
366 args_rels => \%args_rels,
367 deps => {
368 prog => 'magick',
369 },
370 examples => [
371 {
372 summary => 'Change all images to 512px width',
373 src => '[[prog]] 512 *.jpg',
374 src_plang => 'bash',
375 test => 0,
376 'x.doc.show_result' => 0,
377 },
378 ],
379 };
380 0 ▶sub resize_image {
Subroutine resize_image: not called CC=15 SCAR=54.8
Pod documented
381 0 require File::Temp;
382 0 require Image::Size;
383 0 require IPC::System::Options;
384 0 require Perinci::Object;
385  
386 0 my %args = @_;
387  
388 0 my $magick_path = File::Which::which("magick");
389 0 my $size_notation = $args{size_notation};
390  
391 0 ▶ return [412, "Cannot find magick in path"] unless defined $magick_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $magick_path 0 0 0
392  
393 0 my $envres = Perinci::Object::envresmulti();
394 0 my ($num_files, $num_success) = (0, 0);
395 0 my $trash;
396 FILE:
397 0 for my $file (@{$args{files}}) {
398 0 log_info "Processing file %s ...", $file;
399 0 $num_files++;
400  
401 0 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
402 0 log_error "No such file %s, skipped", $file;
403 0 next FILE;
404 }
405  
406 0 my ($width, $height, $fmt) = Image::Size::imgsize($file);
407 0 ▶ unless ($width) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($width) 0 0 0
408 0 log_error "Filename '%s' is not image (%s), skipped", $file, $fmt;
409 0 next FILE;
410 }
411  
412 0 my ($tmpfh, $tmp_file) = File::Temp::tempfile("tmpimgXXXXXXXXX", DIR => ".");
413  
414 0 my @magick_args = (
415 $file,
416 '-resize', $size_notation,
417 $tmp_file,
418 );
419  
420 0 IPC::System::Options::system({log=>1}, $magick_path, @magick_args);
421 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
422 0 $envres->add_result(500, "Failed: $!", {item_id=>$file});
423 0 unlink $tmp_file;
424 0 next FILE;
425 }
426  
427 0 my ($out_width, $out_height, $out_fmt) = Image::Size::imgsize($tmp_file);
428 0 ▶ unless ($out_width) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_width) 0 0 0
429 0 log_error "Can't produce image output at '%s', skipped", $tmp_file;
430 0 unlink $tmp_file;
431 0 next FILE;
432 }
433  
434 0 my ($base, $ext) = $file =~ /^(.*?)(?:\.(\w+))?$/;
435 0 ▶ $ext //= "";
Condition0%0/2
$ext //= ""
l!l
00
MC/DC0%0/1
$ext //= ""
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
436 0 my $out_file = "$base.${out_width}x${out_height}.$ext";
437 0 ▶ if (-f $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $out_file) 0 0 0
438 0 ▶ if ($args{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'overwrite'}) { } 0 0 0
439 0 log_debug "Overwriting $out_file ...";
440 } else {
441 0 $envres->add_result(409, "Not overwriting existing file $out_file", {item_id=>$file});
442 0 unlink $tmp_file;
443 0 next FILE;
444 }
445 }
446  
447 0 log_debug "Renaming %s to %s ...", $tmp_file, $out_file;
448 0 ▶ rename $tmp_file, $out_file or do {
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_file, $out_file or do { $envres->add_result(409, "Not overwriting existing file $out_file", {"item_id", $file}); unlink $tmp_file; next FILE } 0 0 0
449 0 $envres->add_result(409, "Not overwriting existing file $out_file", {item_id=>$file});
450 0 unlink $tmp_file;
451 0 next FILE;
452 };
453  
454 0 $envres->add_result(200, "OK", {item_id=>$file});
455 0 $num_success++;
456  
457 0 ▶ if ($args{trash_original}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'trash_original'}) { } 0 0 0
elsif ($args{'delete_original'}) { } 0 0 0
458 0 require File::Trash::FreeDesktop;
459 0 ▶ $trash //= File::Trash::FreeDesktop->new;
Condition0%0/3
$trash //= "File::Trash::FreeDesktop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$trash //= "File::Trash::FreeDesktop"->new
$trash "File::Trash::FreeDesktop"->new
Truth table0%0/3
A //= B
A= $trashB= "File::Trash::FreeDesktop"->new
ABresult
1X1
011
000
460 0 log_info "Trashing original file %s ...", $file;
461 # will die upon failure, currently we don't trap
462 0 $trash->trash($file);
463 } elsif ($args{delete_original}) {
464 # currently we ignore the result of deletion
465 0 log_info "Deleting original file %s ...", $file;
466 0 unlink $file;
467 }
468 }
469  
470 0 ▶ $num_success == 0 ? [500, "All files failed"] : [200];
Branch: 1 branch
BranchTrueFalseTotal
$num_success == 0 ? : 0 0 0
471 }
472  
473 $SPEC{convert_image_to} = {
474 1 v => 1.1,
475 summary => 'Convert images using ImageMagick\'s \'convert\' utility, with multiple file support and automatic output naming',
476 description => <<'MARKDOWN',
477  
478 This is a simple wrapper to ImageMagick's `convert` utility to let you process
479 multiple files using a single command:
480  
481 % convert-image-to --to pdf *.jpg
482  
483 is basically equivalent to:
484  
485 % for f in *.jpg; do convert "$f" "$f.pdf"; done
486  
487 MARKDOWN
488 args => {
489 %argspec0plus_files,
490 %argspecopt_quality,
491 to => {
492 schema => ['str*', match=>qr/\A\w+\z/],
493 req => 1,
494 examples => [qw/pdf jpg png/], # for tab completion
495 },
496 %argspecs_delete,
497 },
498 #features => {
499 # dry_run => 1,
500 #},
501 deps => {
502 prog => 'convert',
503 },
504 examples => [
505 ],
506 };
507 0 ▶sub convert_image_to {
Subroutine convert_image_to: not called CC=8 SCAR=42.8
Pod documented
508 0 require IPC::System::Options;
509 0 require Perinci::Object;
510 0 require Process::Status;
511  
512 0 my %args = @_;
513  
514 0 ▶ my $to = $args{to} or return [400, "Please specify target format in `to`"];
Branch: 1 branch
BranchTrueFalseTotal
my $to = $args{"to"} or return [400, "Please specify target format in `to`"] 0 0 0
515  
516 0 my $envres = Perinci::Object::envresmulti();
517 0 my $trash;
518 0 for my $file (@{$args{files}}) {
519 0 log_info "Processing file %s ...", $file;
520 IPC::System::Options::system(
521 {log=>1},
522 0 ▶ "convert", ($args{quality} ? ("-quality", $args{quality}) : ()), $file, "$file.$to",
Branch: 1 branch
BranchTrueFalseTotal
$args{'quality'} ? : 0 0 0
523 );
524 0 my $ps = Process::Status->new;
525  
526 0 ▶ if ($ps->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps->is_success) { } 0 0 0
527 0 $envres->add_result(200, "OK", {item_id=>$file});
528 0 ▶ if ($args{trash_original}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'trash_original'}) { } 0 0 0
elsif ($args{'delete_original'}) { } 0 0 0
529 0 require File::Trash::FreeDesktop;
530 0 ▶ $trash //= File::Trash::FreeDesktop->new;
Condition0%0/3
$trash //= "File::Trash::FreeDesktop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$trash //= "File::Trash::FreeDesktop"->new
$trash "File::Trash::FreeDesktop"->new
Truth table0%0/3
A //= B
A= $trashB= "File::Trash::FreeDesktop"->new
ABresult
1X1
011
000
531 0 log_info "Trashing original file %s ...", $file;
532 # will die upon failure, currently we don't trap
533 0 $trash->trash($file);
534 } elsif ($args{delete_original}) {
535 # currently we ignore the result of deletion
536 0 log_info "Deleting original file %s ...", $file;
537 0 unlink $file;
538 }
539 } else {
540 0 $envres->add_result(500, "Failed (exit code ".$ps->exitstatus.")", {item_id=>$file});
541 }
542 }
543 0 $envres->as_struct;
544 }
545  
546 $SPEC{convert_image_to_pdf} = {
547 1 v => 1.1,
548 summary => 'Convert images to PDF using ImageMagick\'s \'convert\' utility',
549 description => <<'MARKDOWN',
550  
551 This is a wrapper to `convert-image-to`, with `--to` set to `pdf`:
552  
553 % convert-image-to-pdf *.jpg
554  
555 is equivalent to:
556  
557 % convert-image-to --to pdf *.jpg
558  
559 which in turn is equivalent to:
560  
561 % for f in *.jpg; do convert "$f" "$f.pdf"; done
562  
563 MARKDOWN
564 args => {
565 %argspec0plus_files,
566 %argspecs_delete,
567 },
568 #features => {
569 # dry_run => 1,
570 #},
571 deps => {
572 prog => 'convert',
573 },
574 examples => [
575 ],
576 };
577 0 ▶sub convert_image_to_pdf {
Subroutine convert_image_to_pdf: not called CC=1 SCAR=6.9
Pod documented
578 0 my %args = @_;
579 0 convert_image_to(%args, to=>'pdf');
580 }
581  
582 $SPEC{convert_image_to_jpg} = {
583 1 v => 1.1,
584 summary => 'Convert images to JPG using ImageMagick\'s \'convert\' utility',
585 description => <<'MARKDOWN',
586  
587 This is a wrapper to `convert-image-to`, with `--to` set to `jpg`:
588  
589 % convert-image-to-pdf *.png
590  
591 is equivalent to:
592  
593 % convert-image-to --to jpg *.png
594  
595 which in turn is equivalent to:
596  
597 % for f in *.png; do convert "$f" "$f.jpg"; done
598  
599 MARKDOWN
600 args => {
601 %argspec0plus_files,
602 %argspecopt_quality,
603 %argspecs_delete,
604 },
605 #features => {
606 # dry_run => 1,
607 #},
608 deps => {
609 prog => 'convert',
610 },
611 examples => [
612 ],
613 };
614 0 ▶sub convert_image_to_jpg {
Subroutine convert_image_to_jpg: not called CC=1 SCAR=6.9
Pod documented
615 0 my %args = @_;
616 0 convert_image_to(%args, to=>'jpg');
617 }
618  
619 $SPEC{convert_image_to_png} = {
620 1 v => 1.1,
621 summary => 'Convert images to JPG using ImageMagick\'s \'convert\' utility',
622 description => <<'MARKDOWN',
623  
624 This is a wrapper to `convert-image-to`, with `--to` set to `png`:
625  
626 % convert-image-to-png *.jpg
627  
628 is equivalent to:
629  
630 % convert-image-to --to png *.jpg
631  
632 which in turn is equivalent to:
633  
634 % for f in *.jpg; do convert "$f" "$f.png"; done
635  
636 MARKDOWN
637 args => {
638 %argspec0plus_files,
639 %argspecopt_quality,
640 %argspecs_delete,
641 },
642 #features => {
643 # dry_run => 1,
644 #},
645 deps => {
646 prog => 'convert',
647 },
648 examples => [
649 ],
650 };
651 0 ▶sub convert_image_to_png {
Subroutine convert_image_to_png: not called CC=1 SCAR=6.9
Pod documented
652 0 my %args = @_;
653 0 convert_image_to(%args, to=>'png');
654 }
655  
656 1 1;
657 # ABSTRACT: Utilities related to ImageMagick
658  
659 __END__
↑ Summary