blib/lib/Image/Resize/Path.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Email-POP3-Markdown-0.01/t/00_use.t ↑ Summary Email-POP3-Markdown-0.01/t/01_accessors.t »
1 package Image::Resize::Path;
2 {
3 $Image::Resize::Path::VERSION = '0.01';
4 }
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use GD::Image;
12 use Carp qw(croak carp);
13  
14 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
15 # STATIC METHODS
16 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
17  
18 sub new
19 # Purpose: Constructor
20 # Input: Ref/String of class
21 # Hash of parameters
22 # Output: Ref to instance
23 {
24 my ( $class, %params) = @_;
25  
26 my $self = bless {}, ref($class) || $class;
27  
28 $self->_init(\%params);
29  
30 return $self;
31 }
32  
33 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
34 # PUBLIC METHODS
35 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
36  
37 __PACKAGE__->mk_accessors(qw(src_path dest_path));
38  
39 sub resize_images
40 # Purpose: Resizes images in a directory
41 # Input: Ref/String of class
42 # Output: An array ref of images resized
43 {
44 my ($self, $width, $height) = @_;
45  
46 if ($width && $height)
47 {
48 return $self->_resize_images($width, $height);
49 }
50 else
51 {
52 croak('A width and height must be specified');
53 }
54
55 }
56  
57 sub supported_images
58 # Purpose: An accessor to for supported_images
59 # Input: Ref of self
60 # Array ref of supported image extensions
61 # Output: Hash ref of support image extensions
62 {
63 my ($self, $images_ar) = @_;
64  
65 if ( $images_ar )
66 {
67 if (ref $images_ar eq 'ARRAY' )
68 {
69 %{$self->{supported_images}} = map { $_ => 1 } @{$images_ar};
70 }
71 else
72 {
73 carp "Parameter must be an array ref. Example: ['gif','jpg']";
74 return;
75 }
76 }
77  
78 return $self->{supported_images};
79 }
80  
81 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
82 # PRIVATE METHODS
83 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
84  
85 sub _init
86 # Purpose: Initializes object state
87 # Input: Ref to self
88 # Hash ref of parameters
89 # Output: Ref to to self
90 {
91 my ($self, $params) = @_;
92  
93 $self->src_path( $params->{src_path} || undef );
94 $self->dest_path ( $params->{dest_path} || undef);
95 $self->supported_images( ['jpg', 'gif', 'png' ] );
96  
97 return $self;
98 }
99  
100 sub _read_path
101 # Purpose: Generates an array ref of valid files to resize
102 # Input: Ref to self
103 # Output: An array ref of files
104 {
105 my ($self) = @_;
106
107 return if !$self->src_path;
108  
109 opendir(DIR, $self->src_path) || die "Unable to open directory: $!\n";
110  
111 my @files = ();
112  
113 foreach my $file(readdir(DIR))
114 {
115 if (my $ext = $self->_validate_file($file))
116 {
117 push @files, [$file, $ext];
118 }
119 }
120  
121 close DIR;
122  
123 return \@files;
124 }
125  
126 sub _resize_images
127 # Purpose: Generates an array ref of valid files to resize
128 # Input: Ref to self
129 # Output: An array ref of files
130 {
131 my ($self, $width, $height) = @_;
132  
133 my $dest_path = $self->dest_path;
134 my $src_path = $self->src_path;
135  
136 if ( $src_path && -e $dest_path )
137 {
138 if ( $src_path eq $dest_path )
139 {
140 croak ("The source path equals the dest path!");
141 return;
142 }
143  
144 my $images_ar = $self->_read_path;
145 my @modified_images = ();
146  
147 for my $image_data ( @{$images_ar} )
148 {
149 my $file_name = $image_data->[0];
150 my $ext = $image_data->[1];
151 my $src_image_obj = GD::Image->new($src_path . '/' . $file_name);
152  
153 my ($img_width, $img_height) = $src_image_obj->getBounds;
154  
155 my $dest_image_obj = GD::Image->new($width, $height);
156  
157 $dest_image_obj->copyResampled(
158 $src_image_obj,
159 0, 0,
160 0, 0,
161 $width, $height,
162 $img_width, $img_height
163 );
164  
165 #$file_name =~ s/$ext/png/o;
166 open (OUT, '>', $dest_path . '/' . $file_name) || croak ("There was a problem opening the file: $file_name $!");
167 binmode OUT;
168  
169 if ( eval{ $dest_image_obj->can($ext) } )
170 {
171 print OUT $dest_image_obj->$ext;
172 push @modified_images, $file_name;
173 }
174  
175 close OUT;
176  
177 if ( $@ )
178 {
179 croak($@);
180 return;
181 }
182  
183
184 }
185  
186 return @modified_images;
187 }
188 else
189 {
190 croak('A destination path must be set');
191 }
192  
193 }
194  
195 sub _validate_file
196 # Purpose: Validates that a file has a proper extension for resizing
197 # Input: Ref to self
198 # String to file name
199 # Output: Returns file name or undef
200 {
201 my ($self, $file) = @_;
202  
203 my $src_path = $self->src_path;
204  
205 my $full_image_path = $src_path . '/' . $file;
206  
207 if (-e $full_image_path && $file =~ m/.+\.(.{3})$/)
208 {
209 my $ext = $1;
210 return $ext if (defined $self->{supported_images}->{$ext} && $self->{supported_images}->{$ext} );
211 }
212 return 0;
213 }
214  
215 1;
216  
217  
218  
219 =pod
220  
221 =head1 NAME
222  
223 Image::Resize::Path - A lightweight wrapper to GD for mass image resizing
224  
225 =head1 VERSION
226  
227 version 0.01
228  
229 =head1 SYNOPSIS
230  
231 use Image::Resize::Path;
232  
233 my $image_obj = Image::Resize::Path->new;
234 $image_obj->supported_images(['jpg']);
235  
236 my $path = './test_data';
237  
238 $test_obj->src_path('/src');
239 $test_obj->dest_path('/dest');
240 $test_obj->resize_images(100,100);
241  
242 =head1 DESCRIPTION
243  
244 Inspired by Image::Resize
245  
246 =head1 STATIC METHODS
247  
248 sub new()
249  
250 Purpose: Constructor
251 Input: Ref/String of class
252 Hash of parameters
253 Output: Ref to instance
254  
255 =head1 PUBLIC METHODS
256  
257 dest_path()
258  
259 Purpose: An accessor for dest_path. This must be set.
260 Input: A string/path value.
261 Output: The current destination path.
262  
263 resize_images()
264  
265 Purpose: Resizes images in a directory.
266 Input: Ref/String of class.
267 Output: An array ref of images resized.
268  
269 supported_images()
270  
271 Purpose: An accessor to for supported_images.
272 Input: Ref of self.
273 Array ref of supported image extensions.
274 Output: Hash ref of support image extensions.
275  
276 src_path()
277  
278 Purpose: An accessor for dest_path. This must be set.
279 Input: A string/path value.
280 Output: The current source path.
281  
282 =head1 AUTHOR
283  
284 Logan Bell <logan@orchardtech.com>
285  
286 =head1 COPYRIGHT AND LICENSE
287  
288 This software is copyright (c) 2011 by Logan Bell.
289  
290 This is free software; you can redistribute it and/or modify it under
291 the same terms as the Perl 5 programming language system itself.
292  
293 =cut
294  
295  
296 __END__
« Email-POP3-Markdown-0.01/t/00_use.t ↑ Summary Email-POP3-Markdown-0.01/t/01_accessors.t »