line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Image::Delivery::Provider; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Image::Delivery::Provider - The abstract Image Provider class |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
An Image Provider is a class that provides images in a way that |
12
|
|
|
|
|
|
|
makes them usable within an L system. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
As well as the actual image data, it provides a variety of metadata |
15
|
|
|
|
|
|
|
that allows the Image::Delivery object to name and store the image correctly. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
4
|
|
22
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
126
|
|
20
|
4
|
|
|
4
|
|
21
|
use Digest::MD5 (); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
56
|
|
21
|
4
|
|
|
4
|
|
18
|
use Digest::TransformPath (); |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
112
|
|
22
|
|
|
|
|
|
|
|
23
|
4
|
|
|
4
|
|
18
|
use vars qw{$VERSION}; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
175
|
|
24
|
|
|
|
|
|
|
BEGIN { |
25
|
4
|
|
|
4
|
|
1872
|
$VERSION = '0.14'; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
##################################################################### |
33
|
|
|
|
|
|
|
# Static Methods |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub filetypes { |
36
|
19
|
|
|
19
|
0
|
65
|
'gif', 'jpg', 'png'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
##################################################################### |
44
|
|
|
|
|
|
|
# Constructor |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Should be implemented in a subclass |
47
|
|
|
|
|
|
|
# sub new |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
##################################################################### |
54
|
|
|
|
|
|
|
# Main Instance Methods |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Object identifier |
57
|
|
|
|
|
|
|
# If the Provider doesn't provide an ID, use a Digest of the |
58
|
|
|
|
|
|
|
# image as a default. |
59
|
|
|
|
|
|
|
sub id { |
60
|
14
|
|
|
14
|
0
|
2972
|
my $self = shift; |
61
|
14
|
100
|
|
|
|
67
|
return $self->{id} if $self->{id}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Create a default id from the image data |
64
|
7
|
|
|
|
|
22
|
my $image = $self->image; |
65
|
7
|
50
|
|
|
|
24
|
ref $image eq 'SCALAR' or return undef; |
66
|
7
|
|
|
|
|
183
|
$self->{id} = Digest::MD5::md5_hex( $$image ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Return the image data |
70
|
|
|
|
|
|
|
### Implemented in subclass |
71
|
|
|
|
|
|
|
# sub image |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Determine the file-type of the image |
74
|
|
|
|
|
|
|
sub content_type { |
75
|
15
|
|
|
15
|
0
|
3516
|
my $self = shift; |
76
|
15
|
100
|
|
|
|
64
|
return $self->{content_type} if $self->{content_type}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Generate the content_type from the image data. |
79
|
|
|
|
|
|
|
# We only need the first 32 bytes to do this. |
80
|
9
|
50
|
|
|
|
40
|
my $image = $self->image or return undef; |
81
|
9
|
|
|
|
|
25
|
my $head = substr($$image, 0, 32); |
82
|
9
|
50
|
|
|
|
32
|
return $self->{content_type} = 'image/jpeg' if $head =~ /^\xFF\xD8/; |
83
|
9
|
50
|
|
|
|
81
|
return $self->{content_type} = 'image/gif' if $head =~ /^GIF8[79]a/; |
84
|
0
|
0
|
|
|
|
0
|
return $self->{content_type} = 'image/png' if $head =~ /^\x89PNG\x0d\x0a\x1a\x0a/; |
85
|
0
|
|
|
|
|
0
|
undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Determine the extention to use |
89
|
|
|
|
|
|
|
sub extension { |
90
|
17
|
|
|
17
|
0
|
3137
|
my $self = shift; |
91
|
17
|
100
|
|
|
|
87
|
return $self->{extension} if $self->{extension}; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Generate the extension from the content_type |
94
|
9
|
50
|
|
|
|
38
|
my $content_type = $self->content_type or return undef; |
95
|
9
|
50
|
|
|
|
61
|
my $extension = { |
96
|
|
|
|
|
|
|
'image/jpeg' => 'jpg', |
97
|
|
|
|
|
|
|
'image/gif' => 'gif', |
98
|
|
|
|
|
|
|
'image/png' => 'png', |
99
|
|
|
|
|
|
|
}->{$content_type} or return undef; |
100
|
9
|
|
|
|
|
55
|
$self->{extension} = $extension; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Return the TransformPath for the Provider |
104
|
|
|
|
|
|
|
sub TransformPath { |
105
|
21
|
|
|
21
|
0
|
3428
|
my $self = shift; |
106
|
21
|
100
|
|
|
|
90
|
return $self->{TransformPath} if $self->{TransformPath}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Create a default TransformPath from the id |
109
|
8
|
50
|
|
|
|
33
|
my $id = $self->id or return undef; |
110
|
8
|
|
|
|
|
73
|
$self->{TransformPath} = Digest::TransformPath->new($id); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
##################################################################### |
118
|
|
|
|
|
|
|
# Coercion Support |
119
|
|
|
|
|
|
|
|
120
|
13
|
|
|
13
|
|
350
|
sub __as_Digest_TransformPath { shift->TransformPath } |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |