Uploader/Transform/ImageMagick.pm

filter: Statementstmt 25.0% 17 / 68 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 18.1% 23 / 127 CC 20 SCAR 50.3
CC
20
Cov
16%
_really_gen_thumb
272.0
_calc_target_size
12.0
gen_thumb
8.0
CRAP 152.5
↑ Summary Uploader.pm »
1 package CGI::Uploader::Transform::ImageMagick;
2  
3 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Temp 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = 2.18;
9 1 our @EXPORT = qw(&gen_thumb);
10  
11 =head2 gen_thumb()
12  
13 use CGI::Uploader::Transform::ImageMagick;
14  
15 As a class method:
16  
17 ($thumb_tmp_filename) = CGI::Uploader::Transform::ImageMagick->gen_thumb({
18 filename => $orig_filename,
19 w => $width,
20 h => $height
21 });
22  
23 Within a CGI::Uploader C<spec>:
24  
25 gen_files => {
26 my_thumb => gen_thumb({ w => $width, h => $height }),
27 }
28  
29 Looking for a different syntax? See L<BACKWARDS COMPATIBILITY>
30  
31 This function creates a copy of given image file and resizes the copy to the
32 provided width and height.
33  
34 C<gen_thumb> can be called as object or class method. As a class method,
35 there there is no need to call C<new()> before calling this method.
36  
37 L<Graphics::Magick> is used as the first choice image service module.
38 L<Image::Magick> is tried next.
39  
40 Input:
41  
42 filename - filename of source image
43 w - max width of thumbnail
44 h - max height of thumbnail
45  
46 One or both of C<w> or C<h> is required.
47  
48 Output:
49 - filename of generated tmp file for the thumbnail
50 - the initialized image generation object. (You generally shouldn't need this)
51  
52 =cut
53  
54 0 ▶sub gen_thumb {
Subroutine gen_thumb: not called CC=3 SCAR=20.8
Pod documented
55 # If the first arg is an object, we have really work to do right now
56 0 my $first_arg = $_[0];
57 1 ▶ use Scalar::Util (qw/blessed/);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 0 ▶ if ((blessed $first_arg) or (eval {$first_arg->can('gen_thumb')})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($first_arg) or eval { do { $first_arg->can('gen_thumb') } }) { } 0 0 0
Condition0%0/3
builtin::blessed($first_arg) or eval { do { $first_arg->can("gen_thumb") } }
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::blessed($first_arg) or eval { do { $first_arg->can("gen_thumb") } }
builtin::blessed($first_arg) eval { do { $first_arg->can("gen_thumb") } }
Truth table0%0/3
A or B
A= builtin::blessed($first_arg)B= eval { do { $first_arg->can("gen_thumb") } }
ABresult
1X1
011
000
59 0 return _really_gen_thumb(@_);
60 }
61 # Otherwise, just generate a closure pass back a code ref for later use
62 else {
63 # require a single hashref as input
64 0 my ($args_href) = validate_pos(@_, { type => HASHREF });
65 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my $self = shift;
67 0 my $filename = shift;
68 0 _really_gen_thumb($self, {
69 filename => $filename,
70 %$args_href,
71 });
72 }
73 0 }
74 }
75  
76 0 ▶sub _really_gen_thumb {
Subroutine _really_gen_thumb: not called CC=16 SCAR=56.1
77 0 ▶ my $self = shift || die "gen_thumb needs object";
Condition0%0/2
shift() || die('gen_thumb needs object')
l!l
00
MC/DC0%0/1
shift() || die('gen_thumb needs object')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
78 0 my (%p,$orig_filename,$params);
79 # If we have the new hashref API
80 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
81 0 %p = validate(@_,{
82 filename => { type => SCALAR },
83 w => { type => SCALAR | UNDEF, regex => qr/^\d*$/, optional => 1, },
84 h => { type => SCALAR | UNDEF, regex => qr/^\d*$/, optional => 1 },
85 });
86 0 $orig_filename = $p{filename};
87 }
88 # we have the old ugly style API
89 else {
90 0 ($orig_filename, $params) = validate_pos(@_,1,{ type => ARRAYREF });
91 # validate handles a hash or hashref transparently
92 0 %p = validate(@$params,{
93 w => { type => SCALAR | UNDEF, regex => qr/^\d*$/, optional => 1, },
94 h => { type => SCALAR | UNDEF, regex => qr/^\d*$/, optional => 1 },
95 });
96 }
97 0 ▶ die "must supply 'w' or 'h'" unless (defined $p{w} or defined $p{h});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{"w"} or defined $p{"h"} 0 0 0
Condition0%0/3
defined $p{"w"} or defined $p{"h"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $p{"w"} or defined $p{"h"}
defined $p{"w"} defined $p{"h"}
Truth table0%0/3
A or B
A= defined $p{"w"}B= defined $p{"h"}
ABresult
1X1
011
000
98  
99 # Having both Graphics::Magick and Image::Magick loaded at the same time
100 # can cause very strange problems, so we take care to avoid that
101 # First see if we have already loaded Graphics::Magick or Image::Magick
102 # If so, just use whichever one is already loaded.
103 0 my $magick_module;
104 0 ▶ if (exists $INC{'Graphics/Magick.pm'}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $INC{'Graphics/Magick.pm'}) { } 0 0 0
elsif (exists $INC{'Image/Magick.pm'}) { } 0 0 0
elsif (_load_magick_module('Graphics::Magick')) { } 0 0 0
elsif (_load_magick_module('Image::Magick')) { } 0 0 0
105 0 $magick_module = 'Graphics::Magick';
106 }
107 elsif (exists $INC{'Image/Magick.pm'}) {
108 0 $magick_module = 'Image::Magick';
109 }
110  
111 # If neither are already loaded, try loading either one.
112 elsif ( _load_magick_module('Graphics::Magick') ) {
113 0 $magick_module = 'Graphics::Magick';
114 }
115 elsif ( _load_magick_module('Image::Magick') ) {
116 0 $magick_module = 'Image::Magick';
117 }
118 else {
119 0 die "No graphics module found for image resizing. Install Graphics::Magick or Image::Magick: $@ "
120 }
121  
122 0 my ($thumb_tmp_fh, $thumb_tmp_filename) = tempfile('CGIuploaderXXXXX', UNLINK => 1, DIR => $self->{'temp_dir'});
123 0 binmode($thumb_tmp_fh);
124  
125 0 my $img = $magick_module->new();
126  
127 0 my $err;
128 0 eval {
129 0 $err = $img->Read(filename=>$orig_filename);
130 0 ▶ die "Error while reading $orig_filename: $err" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
131  
132 0 my ($target_w,$target_h) = _calc_target_size($img,$p{w},$p{h});
133  
134 0 $err = $img->Resize($target_w.'x'.$target_h);
135 0 ▶ die "Error while resizing $orig_filename: $err" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
136 0 $err = $img->Write($thumb_tmp_filename);
137 0 ▶ die "Error while writing $orig_filename: $err" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
138 };
139 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
140 0 warn $@;
141 0 my $code;
142 # codes > 400 are fatal
143 0 ▶ die $err if ((($code) = $err =~ /(\d+)/) and ($code > 400));
Branch: 1 branch
BranchTrueFalseTotal
if ($code) = $err =~ /(\d+)/ and $code > 400 0 0 0
Condition0%0/3
($code) = $err =~ /(\d+)/ and $code > 400
!ll&&!rl&&r
000
MC/DC0%0/2
($code) = $err =~ /(\d+)/ and $code > 400
($code) = $err =~ /(\d+)/ $code > 400
Truth table0%0/3
A and B
A= ($code) = $err =~ /(\d+)/B= $code > 400
ABresult
0X0
100
111
144 }
145  
146 0 assert ($thumb_tmp_filename, 'thumbnail tmp file created');
147 0 ▶ return wantarray ? ($thumb_tmp_filename, $img ) : $thumb_tmp_filename;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
148  
149 }
150  
151  
152 # Calculate the target with height
153 #
154 # my ($target_w,$target_h) = _calc_target_size($img,$p{w},$p{h})
155 #
156 # Input:
157 #
158 # - Magick object, pre-opened with the original file
159 # - provided width
160 # - provided height
161  
162 0 ▶sub _calc_target_size {
Subroutine _calc_target_size: not called CC=3 SCAR=24.8
163 0 my ($img,$w,$h) = @_;
164  
165 0 my $target_h = $h;
166 0 my $target_w = $w;
167 0 my ($orig_w,$orig_h) = $img->Get('width','height');
168  
169 0 ▶ $target_h = sprintf("%.1d", ($orig_h * $target_w) / $orig_w) unless $target_h;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_h 0 0 0
170 0 ▶ $target_w = sprintf("%.1d", ($orig_w * $target_h) / $orig_h) unless $target_w;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_w 0 0 0
171  
172 0 return ($target_w,$target_h);
173  
174 }
175  
176  
177  
178  
179 # load Graphics::Magick or Image::Magick if one is not already loaded.
180 0 ▶sub _load_magick_module {
Subroutine _load_magick_module: not called CC=1 SCAR=6.9
181 0 my $module_name = shift;
182 0 return eval "require $module_name";
183 }
184  
185 =head2 BACKWARDS COMPATIBILITY
186  
187 These older, more awkward syntaxes are still supported:
188  
189 As a class method:
190  
191 ($thumb_tmp_filename) = CGI::Uploader::Transform::ImageMagick->gen_thumb(
192 $orig_filename,
193 [ w => $width, h => $height ]
194 );
195  
196 In a C<CGI::Uploader> C<spec>:
197  
198 'my_img_field_name' => {
199 transform_method => \&gen_thumb,
200 params => [ w => 100, h => 100 ],
201 }
202  
203  
204 1;
↑ Summary Uploader.pm »