File Coverage

blib/lib/Image/Select.pm
Criterion Covered Total %
statement 47 89 52.8
branch 7 30 23.3
condition 2 6 33.3
subroutine 11 14 78.5
pod 4 4 100.0
total 71 143 49.6


line stmt bran cond sub pod time code
1             package Image::Select;
2              
3 5     5   108726 use strict;
  5         31  
  5         147  
4 5     5   24 use warnings;
  5         9  
  5         135  
5              
6 5     5   1862 use Class::Utils qw(set_params);
  5         111017  
  5         110  
7 5     5   280 use Error::Pure qw(err);
  5         10  
  5         198  
8 5     5   28 use File::Basename qw(fileparse);
  5         9  
  5         435  
9 5     5   2755 use File::Find::Rule qw(:MMagic);
  5         40433  
  5         34  
10 5     5   94003 use Imager;
  5         173455  
  5         42  
11 5     5   315 use List::MoreUtils qw(none);
  5         11  
  5         47  
12              
13             our $VERSION = 0.05;
14              
15             # Constructor.
16             sub new {
17 6     6 1 6840 my ($class, @params) = @_;
18              
19             # Create object.
20 6         17 my $self = bless {}, $class;
21              
22             # Debug.
23 6         16 $self->{'debug'} = 0;
24              
25             # Loop.
26 6         11 $self->{'loop'} = 0;
27              
28             # Path to images.
29 6         10 $self->{'path_to_images'} = undef;
30              
31             # Image type.
32 6         11 $self->{'type'} = 'bmp';
33              
34             # Sizes.
35 6         10 $self->{'height'} = 1080;
36 6         20 $self->{'width'} = 1920;
37              
38             # Process params.
39 6         20 set_params($self, @params);
40              
41             # Check type.
42 4 100       59 if (defined $self->{'type'}) {
43 3         11 $self->_check_type($self->{'type'});
44             }
45              
46             # Check path to images.
47 3 100 66     52 if (! defined $self->{'path_to_images'}
48             || ! -d $self->{'path_to_images'}) {
49              
50 1         4 err "Parameter 'path_to_images' is required.";
51             }
52              
53             # Load images.
54             $self->{'_images_to_select'} = [
55             sort File::Find::Rule->file->magic(
56             'image/bmp',
57             'image/gif',
58             'image/jpeg',
59             'image/png',
60             'image/tiff',
61             'image/x-ms-bmp',
62             'image/x-portable-pixmap',
63             # XXX tga?
64             # XXX raw?
65             # XXX sgi?
66 2         65 )->in($self->{'path_to_images'}),
67             ];
68 2 50       44732 if (! @{$self->{'_images_to_select'}}) {
  2         11  
69 0         0 err 'No images.';
70             }
71 2         6 $self->{'_images_index'} = 0;
72              
73             # Object.
74 2         12 return $self;
75             }
76              
77             # Create image.
78             sub create {
79 0     0 1 0 my ($self, $path) = @_;
80              
81             # Load next image.
82 0         0 my $i = Imager->new;
83 0 0       0 if ($self->{'_images_index'} > $#{$self->{'_images_to_select'}}) {
  0         0  
84 0 0       0 if ($self->{'loop'}) {
85 0         0 $self->{'_images_index'} = 0;
86             } else {
87 0         0 return;
88             }
89             }
90 0         0 my $file = $self->{'_images_to_select'}->[$self->{'_images_index'}];
91 0 0       0 if (! -r $file) {
92 0         0 err "No file '$file'.";
93             }
94 0         0 my $ret = $i->read('file' => $file);
95 0 0       0 if (! $ret) {
96 0         0 err "Cannot read file '$file'.",
97             'Error', Imager->errstr;
98             }
99 0         0 $self->{'_images_index'}++;
100              
101             # Get type.
102 0         0 my $suffix;
103 0 0       0 if (! defined $self->{'type'}) {
104              
105             # Get suffix.
106 0         0 (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms);
107 0         0 $suffix =~ s/^\.//ms;
108              
109             # Jpeg.
110 0 0       0 if ($suffix eq 'jpg') {
111 0         0 $suffix = 'jpeg';
112             }
113              
114             # Check type.
115 0         0 $self->_check_type($suffix);
116             } else {
117 0         0 $suffix = $self->{'type'};
118             }
119              
120             # Scale.
121             my $new_i = $i->scale(
122             'xpixels' => $self->{'width'},
123 0         0 'ypixels' => $self->{'height'},
124             );
125 0 0       0 if (! $new_i) {
126 0         0 err "Cannot resize image from file '$file'.",
127             'Error', Imager->errstr;
128             }
129              
130             # Save.
131 0 0       0 if ($self->{'debug'}) {
132 0         0 print "Path: $path\n";
133             }
134 0         0 $ret = $new_i->write(
135             'file' => $path,
136             'type' => $suffix,
137             );
138 0 0       0 if (! $ret) {
139 0         0 err "Cannot write file to '$path'.",
140             'Error', Imager->errstr;
141             }
142              
143 0         0 return $suffix;
144             }
145              
146             # Set/Get image sizes.
147             sub sizes {
148 0     0 1 0 my ($self, $width, $height) = @_;
149 0 0 0     0 if ($width && $height) {
150 0         0 $self->{'width'} = $width;
151 0         0 $self->{'height'} = $height;
152             }
153 0         0 return ($self->{'width'}, $self->{'height'});
154             }
155              
156             # Set/Get image type.
157             sub type {
158 0     0 1 0 my ($self, $type) = @_;
159 0 0       0 if ($type) {
160 0         0 $self->_check_type($type);
161 0         0 $self->{'type'} = $type;
162             }
163 0         0 return $self->{'type'};
164             }
165              
166             # Check supported image type.
167             sub _check_type {
168 3     3   14 my ($self, $type) = @_;
169            
170             # Check type.
171 3 100   11   19 if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
  11         19  
172             'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
173              
174 1         6 err "Image type '$type' doesn't supported.";
175             }
176              
177 2         8 return;
178             }
179              
180             1;
181              
182             __END__