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 5     5   59294 use strict;
  5         11  
  5         123  
5 5     5   23 use warnings;
  5         13  
  5         137  
6              
7             # Modules.
8 5     5   2938 use Class::Utils qw(set_params);
  5         111530  
  5         165  
9 5     5   279 use Error::Pure qw(err);
  5         12  
  5         286  
10 5     5   27 use File::Basename qw(fileparse);
  5         9  
  5         337  
11 5     5   4284 use File::Find::Rule qw(:MMagic);
  5         45215  
  5         34  
12 5     5   113296 use Imager;
  5         187577  
  5         42  
13 5     5   378 use List::MoreUtils qw(none);
  5         13  
  5         60  
14              
15             # Version.
16             our $VERSION = 0.04;
17              
18             # Constructor.
19             sub new {
20 6     6 1 6316 my ($class, @params) = @_;
21              
22             # Create object.
23 6         13 my $self = bless {}, $class;
24              
25             # Debug.
26 6         20 $self->{'debug'} = 0;
27              
28             # Loop.
29 6         9 $self->{'loop'} = 0;
30              
31             # Path to images.
32 6         14 $self->{'path_to_images'} = undef;
33              
34             # Image type.
35 6         11 $self->{'type'} = 'bmp';
36              
37             # Sizes.
38 6         12 $self->{'height'} = 1080;
39 6         10 $self->{'width'} = 1920;
40              
41             # Process params.
42 6         19 set_params($self, @params);
43              
44             # Check type.
45 4 100       54 if (defined $self->{'type'}) {
46 3         11 $self->_check_type($self->{'type'});
47             }
48              
49             # Check path to images.
50 3 100 66     65 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       35424 if (! @{$self->{'_images_to_select'}}) {
  2         10  
72 0         0 err 'No images.';
73             }
74 2         8 $self->{'_images_index'} = 0;
75              
76             # Object.
77 2         9 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   5 my ($self, $type) = @_;
172            
173             # Check type.
174 3 100   11   17 if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
  11         21  
175             'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
176              
177 1         6 err "Image type '$type' doesn't supported.";
178             }
179              
180 2         7 return;
181             }
182              
183             1;
184              
185             __END__