| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Validate::Image; |
|
2
|
1
|
|
|
1
|
|
15756
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
31
|
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
41
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.012002'; |
|
6
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
496
|
use Image::Info; |
|
|
1
|
|
|
|
|
1365
|
|
|
|
1
|
|
|
|
|
348
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new{ |
|
11
|
1
|
|
|
1
|
0
|
439
|
my $incovant = shift; |
|
12
|
1
|
|
33
|
|
|
5
|
my $class = ref( $incovant ) || $incovant; |
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
|
|
|
2
|
my $self = {}; |
|
15
|
1
|
|
|
|
|
2
|
bless( $self, $class ); |
|
16
|
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
3
|
return $self; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub validate{ |
|
21
|
17
|
|
|
17
|
1
|
10989
|
my ( $self, $file ) = @_; |
|
22
|
|
|
|
|
|
|
|
|
23
|
17
|
|
|
|
|
41
|
my $image_type = Image::Info::image_info($file); |
|
24
|
|
|
|
|
|
|
|
|
25
|
17
|
100
|
|
|
|
72373
|
if ( !defined( $image_type->{'file_ext'} ) ){ |
|
26
|
8
|
|
|
|
|
19
|
return undef; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
9
|
|
|
|
|
205
|
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
|
|
|
|
22
|
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
|
|
|
|
|
48
|
return $image_info; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _convert_installed{ |
|
53
|
13
|
|
|
13
|
|
5344
|
my ( $self ) = @_; |
|
54
|
|
|
|
|
|
|
|
|
55
|
13
|
|
|
|
|
307
|
my @paths = split( /:|;/, $ENV{PATH} ); |
|
56
|
13
|
|
|
|
|
22
|
foreach my $path ( @paths ){ |
|
57
|
91
|
50
|
33
|
|
|
641
|
if ( |
|
58
|
|
|
|
|
|
|
( -e "${path}/convert" ) |
|
59
|
|
|
|
|
|
|
&& ( -x "${path}/convert" ) |
|
60
|
|
|
|
|
|
|
){ |
|
61
|
0
|
|
|
|
|
0
|
return 1; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
13
|
|
|
|
|
40
|
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 SYNOPSIS |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
use Data::Validate::Image; |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $validator = Data::Validate::Image->new(); |
|
79
|
|
|
|
|
|
|
my $image_info = $validator->validate( '/path/to/image' ); |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
if ( defined( $image_info ) ){ |
|
82
|
|
|
|
|
|
|
#valid image, do things here |
|
83
|
|
|
|
|
|
|
} else { |
|
84
|
|
|
|
|
|
|
#invalid image |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
A simple image validator class, which provides a single C method |
|
90
|
|
|
|
|
|
|
that upon success returns a hash ref of image properties: |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
{ |
|
93
|
|
|
|
|
|
|
'width' => 'image width', |
|
94
|
|
|
|
|
|
|
'height' => 'image height', |
|
95
|
|
|
|
|
|
|
'size' => 'image filesize (KB)', |
|
96
|
|
|
|
|
|
|
'mime' => 'image mime type', |
|
97
|
|
|
|
|
|
|
'file_ext' => '*correct* file extenstion', |
|
98
|
|
|
|
|
|
|
'frames' => 'frame count', # requires convert from ImageMagick |
|
99
|
|
|
|
|
|
|
'animated' => 1 || 0, # requires convert from ImageMagick |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
For invalid images the C method returns C. |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 IMPORTANT |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
B convert (from L) to |
|
107
|
|
|
|
|
|
|
detect the C and C properties. |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
I used convert over PerlMagick because I found PerlMagick to be very |
|
110
|
|
|
|
|
|
|
unstable. |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 METHODS |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 validate |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Returns image properties for valid image or C for invalid image |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Mark Ellis Emarkellis@cpan.orgE |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
j1n3l0 - Nelo Onyiah - Enelo.onyiah@gmail.comE |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 LICENSE |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Copyright 2015 Mark Ellis Emarkellis@cpan.orgE |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify |
|
135
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |