script/autocrop.pl untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 172 CC 21 SCAR 58.9
CC
21
Cov
0%
run
462.0
usage_and_exit
2.0
BEGIN
2.0
CRAP 359.8
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Imager;
5 use Imager::Filter::Autocrop;
6 use Getopt::Long;
7  
8 my $VERSION = '1.23';
9  
10 binmode(STDOUT, ":encoding(UTF-8)");
11 if (my $rv = run()) {
12 print "$rv\n";
13 exit 255;
14 }
15 exit 0;
16  
17 sub run {
18 my %opt = ();
19 my $args = @ARGV;
20 GetOptions (\%opt, 'in=s', 'out=s', 'color=s', 'fuzz=i', 'border=i', 'jpegquality=i', 'detect', 'silent', 'help') || return "Use --help to see available parameters.";
21 usage_and_exit() unless ($args and !$opt{'help'});
22 my %detect = ();
23 print "[#] Imager::Filter::Autocrop v$VERSION\n" unless $opt{silent};
24 return "Color is not specified correctly." if (defined $opt{color} and $opt{color}!~/^(#)?[a-fA-f0-9]{3}(?:[a-fA-f0-9]{3})?$/);
25 $opt{color} = "#$opt{color}" if (defined $opt{color} and !$1);
26 return "Fuzz is not specified correctly." if ($opt{fuzz} and ($opt{fuzz}=~/\D/ or $opt{fuzz} > 255));
27 return "Border is not specified correctly." if ($opt{border} and $opt{border}=~/\D/);
28 return "JPEG quality is not specified correctly." if ($opt{jpegquality} and ($opt{jpegquality}=~/\D/ or $opt{jpegquality} > 100));
29 return "Source image should be specified." unless $opt{in};
30 return "Source image is not readable." unless (-f $opt{in} and -r _);
31 return "Output image name should be specified." if (!$opt{out} and !$opt{detect});
32 print "Processing image '$opt{in}' - " . ($opt{detect} ? "detecting" : "cropping") . ".\n" unless $opt{silent};
33 print "Using color " . ($opt{color} ? uc("'$opt{color}'") : "from top left corner.") . ($opt{fuzz} ? " Fuzz: $opt{fuzz}." : "") . ($opt{border} ? " Border: $opt{border}." : "") . "\n" unless $opt{silent};
34
35 my $img = Imager->new();
36 $img->read(file => $opt{in}) or return $img->errstr;
37 my %runtime = ();
38 $runtime{detect} = \%detect if $opt{detect};
39 foreach (qw<color fuzz border>) {
40 $runtime{$_} = $opt{$_} if $opt{$_};
41 }
42 $img->filter(type => 'autocrop', %runtime) or return $img->errstr;
43 if ($opt{detect}) {
44 print join(", ", map { "$_: " . $detect{$_}||'unknown' } qw<top left bottom right>), "\n";
45 } else {
46 %runtime = ();
47 foreach (qw<jpegquality>) {
48 $runtime{$_} = $opt{$_} if $opt{$_};
49 }
50 $img->write(file => $opt{out}, %runtime) or return $img->errstr;
51 }
52 return;
53 }
54  
55 sub usage_and_exit {
56 local $/;
57 print <DATA>;
58 exit(1);
59 }
60  
61 __END__