| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Validate::Image; |
|
2
|
1
|
|
|
1
|
|
19268
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
40
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.012001'; |
|
6
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
893
|
use Image::Info; |
|
|
1
|
|
|
|
|
1436
|
|
|
|
1
|
|
|
|
|
388
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new{ |
|
11
|
1
|
|
|
1
|
0
|
757
|
my $incovant = shift; |
|
12
|
1
|
|
33
|
|
|
8
|
my $class = ref( $incovant ) || $incovant; |
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
|
|
|
2
|
my $self = {}; |
|
15
|
1
|
|
|
|
|
3
|
bless( $self, $class ); |
|
16
|
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
4
|
return $self; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub validate{ |
|
21
|
17
|
|
|
17
|
1
|
22840
|
my ( $self, $file ) = @_; |
|
22
|
|
|
|
|
|
|
|
|
23
|
17
|
|
|
|
|
64
|
my $image_type = Image::Info::image_info($file); |
|
24
|
|
|
|
|
|
|
|
|
25
|
17
|
100
|
|
|
|
134073
|
if ( !defined( $image_type->{'file_ext'} ) ){ |
|
26
|
8
|
|
|
|
|
25
|
return undef; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
9
|
|
|
|
|
329
|
my $image_info = { |
|
30
|
|
|
|
|
|
|
'width' => $image_type->{'width'}, |
|
31
|
|
|
|
|
|
|
'height' => $image_type->{'height'}, |
|
32
|
|
|
|
|
|
|
'size' => (-s $file) / 1024, |
|
33
|
|
|
|
|
|
|
'mime' => $image_type->{'file_media_type'}, |
|
34
|
|
|
|
|
|
|
'file_ext' => $image_type->{'file_ext'}, |
|
35
|
|
|
|
|
|
|
}; |
|
36
|
|
|
|
|
|
|
|
|
37
|
9
|
50
|
|
|
|
31
|
if ( $self->_convert_installed ){ #test if imagemagic is installed |
|
38
|
0
|
|
|
|
|
0
|
my @frames = `convert -identify '${file}' null: 2> /dev/null`; |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
0
|
$image_info->{'frames'} = scalar( @frames ); |
|
41
|
0
|
0
|
|
|
|
0
|
$image_info->{'animated'} = ($#frames) ? 1 : 0; |
|
42
|
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
0
|
if ( $? ){ |
|
44
|
|
|
|
|
|
|
# convert returns 0 on success - prolly a corrupt image |
|
45
|
0
|
|
|
|
|
0
|
return undef; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
9
|
|
|
|
|
70
|
return $image_info; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _convert_installed{ |
|
53
|
13
|
|
|
13
|
|
8949
|
my ( $self ) = @_; |
|
54
|
|
|
|
|
|
|
|
|
55
|
13
|
|
|
|
|
193
|
my @paths = split( /:|;/, $ENV{PATH} ); |
|
56
|
13
|
|
|
|
|
32
|
foreach my $path ( @paths ){ |
|
57
|
91
|
50
|
33
|
|
|
1511
|
if ( |
|
58
|
|
|
|
|
|
|
( -e "${path}/convert" ) |
|
59
|
|
|
|
|
|
|
&& ( -x "${path}/convert" ) |
|
60
|
|
|
|
|
|
|
){ |
|
61
|
0
|
|
|
|
|
0
|
return 1; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
13
|
|
|
|
|
54
|
return 0; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 NAME |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Data::Validate::Image - Validates an image and returns basic info |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 IMPORTANT |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
B convert (from imagemagick) to be installed and in the path for animated gif/frame detection |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
I used convert over PerlMagick because I found PerlMagick to be very unstable. |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
use Data::Validate::Image; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $validator = Data::Validate::Image->new(); |
|
85
|
|
|
|
|
|
|
my $image_info = $validator->validate( '/path/to/image' ); |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
if ( defined( $image_info ) ){ |
|
88
|
|
|
|
|
|
|
#valid image, do things here |
|
89
|
|
|
|
|
|
|
} else { |
|
90
|
|
|
|
|
|
|
#invalid image |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
pretty simple image validator class. returns hash of image properties on success, |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
undef for invalid images |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
hash properties are |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
'width' => image width, |
|
102
|
|
|
|
|
|
|
'height' => image height, |
|
103
|
|
|
|
|
|
|
'size' => image filesize (KB), |
|
104
|
|
|
|
|
|
|
'mime' => image mime type, |
|
105
|
|
|
|
|
|
|
'file_ext' => *correct* file extenstion, |
|
106
|
|
|
|
|
|
|
'frames' => frame count, #requires convert from imagemagic to be installed |
|
107
|
|
|
|
|
|
|
'animated' => 1 || 0, #requires convert from imagemagic to be installed |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 METHODS |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 validate |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
returns image info or undef for invalid image |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 AUTHORS |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Mark Ellis Emarkellis@cpan.orgE |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
L |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 LICENSE |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Copyright 2014 Mark Ellis Emarkellis@cpan.orgE |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify |
|
128
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |