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