line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Image::Empty; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
133985
|
use 5.006; |
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
181
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
26
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
150
|
|
6
|
4
|
|
|
4
|
|
23
|
use warnings; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
124
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
2336
|
use MIME::Base64; |
|
4
|
|
|
|
|
2295
|
|
|
4
|
|
|
|
|
3179
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Image::Empty - HTTP response helper for 1x1 empty GIFs or PNGs, for building tracking URLs. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.20 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.20'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Create 1x1 empty/transparent GIFs or PNGs to use in tracking URLs without the hassle of actually creating and/or loading image data. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Such a basic and common scenario deserves a basic solution. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Image::Empty; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $gif = Image::Empty->gif; # swap for png |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
print $gif->render; # HTTP headers and body |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 METHODS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 Class Methods |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new |
45
|
|
|
|
|
|
|
{ |
46
|
3
|
|
|
3
|
0
|
76
|
my ( $class, %args ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $self = { type => $args{ type }, |
49
|
|
|
|
|
|
|
length => $args{ length }, |
50
|
|
|
|
|
|
|
disposition => $args{ disposition }, |
51
|
|
|
|
|
|
|
filename => $args{ filename }, |
52
|
|
|
|
|
|
|
content => $args{ content }, |
53
|
3
|
|
|
|
|
25
|
}; |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
8
|
bless( $self, $class ); |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
21
|
return $self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head3 gif |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Returns an instance representing an empty GIF (43 bytes) for use in an HTTP response. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $gif = Image::Empty->gif; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub gif |
69
|
|
|
|
|
|
|
{ |
70
|
1
|
|
|
1
|
1
|
52
|
my ( $class, %args ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
19
|
return $class->new( type => 'image/gif', |
73
|
|
|
|
|
|
|
length => 43, |
74
|
|
|
|
|
|
|
disposition => 'inline', |
75
|
|
|
|
|
|
|
filename => 'empty.gif', |
76
|
|
|
|
|
|
|
content => decode_base64('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head3 png |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Returns an instance representing an empty PNG (67 bytes) for use in an HTTP response. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $png = Image::Empty->png; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub png |
90
|
|
|
|
|
|
|
{ |
91
|
1
|
|
|
1
|
1
|
56
|
my ( $class, %args ) = @_; |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
20
|
return $class->new( type => 'image/png', |
94
|
|
|
|
|
|
|
length => 67, |
95
|
|
|
|
|
|
|
disposition => 'inline', |
96
|
|
|
|
|
|
|
filename => 'empty.png', |
97
|
|
|
|
|
|
|
content => decode_base64('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=='), |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 Instance Methods |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head3 render |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
The C method is used to set the HTTP headers and body. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$gif->render; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
With no arguments, returns a string. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Under a CGI environment this would generally be printed directly to C (ie, the browser). |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Chaining methods together can give very concise and compact code. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
use Image::Empty; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
print Image::Empty->gif->render; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Remember that the C method returns the HTTP headers. The above 2 lines are all you need in a script. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head4 Plack |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
If you are working with Plack, supply the L object to the C method. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The C L object is returned. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
As a quick one-liner... |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $app = sub { |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return Image::Empty->gif->render( Plack::Response->new ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
It is the same as doing... |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $app = sub { |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my $gif = Image::Empty->gif; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $response = Plack::Response->new; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$response->status(200); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
$response->content_type( $gif->type ); |
145
|
|
|
|
|
|
|
$response->content_length( $gif->length ); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$response->header( 'Content-Disposition' => $gif->disposition . '; filename="' . $gif->filename . '"' ); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$response->body( $gif->content ); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
return $response->finalize; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub render |
157
|
|
|
|
|
|
|
{ |
158
|
2
|
|
|
2
|
1
|
6
|
my ( $self, $handler ) = @_; |
159
|
|
|
|
|
|
|
|
160
|
2
|
50
|
|
|
|
8
|
if ( ref $handler eq 'Plack::Response' ) |
161
|
|
|
|
|
|
|
{ |
162
|
0
|
|
|
|
|
0
|
$handler->status(200); |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
0
|
$handler->content_type( $self->type ); |
165
|
0
|
|
|
|
|
0
|
$handler->content_length( $self->length ); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
0
|
$handler->header( 'Content-Disposition' => $self->disposition . '; filename="' . $self->filename . '"' ); |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
0
|
$handler->body( $self->content ); |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
0
|
return $handler->finalize; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
2
|
|
|
|
|
14
|
return 'Content-Type: ' . $self->type . "\015\012" . |
175
|
|
|
|
|
|
|
'Content-Length: ' . $self->length . "\015\012" . |
176
|
|
|
|
|
|
|
'Content-Disposition: ' . $self->disposition . '; filename="' . $self->filename . '"' . "\015\012" . |
177
|
|
|
|
|
|
|
"\015\012" . |
178
|
|
|
|
|
|
|
$self->content; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head4 Catalyst |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
If you are working with Catalyst, see L. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 Attributes |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head3 type |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
$gif->type; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Returns the mime/type of the image for use in HTTP headers. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub type |
196
|
|
|
|
|
|
|
{ |
197
|
4
|
|
|
4
|
1
|
2564
|
my ( $self, $arg ) = @_; |
198
|
4
|
50
|
|
|
|
17
|
$self->{ type } = $arg if defined $arg; |
199
|
4
|
|
|
|
|
38
|
return $self->{ type }; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head3 length |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
$gif->length; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Returns the content length for use in HTTP headers. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub length |
211
|
|
|
|
|
|
|
{ |
212
|
4
|
|
|
4
|
1
|
11
|
my ( $self, $arg ) = @_; |
213
|
4
|
50
|
|
|
|
12
|
$self->{ length } = $arg if defined $arg; |
214
|
4
|
|
|
|
|
25
|
return $self->{ length }; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head3 disposition |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
$gif->disposition; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Returns the content disposition for use in HTTP headers. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub disposition |
226
|
|
|
|
|
|
|
{ |
227
|
4
|
|
|
4
|
1
|
8
|
my ( $self, $arg ) = @_; |
228
|
4
|
50
|
|
|
|
19
|
$self->{ disposition } = $arg if defined $arg; |
229
|
4
|
|
|
|
|
22
|
return $self->{ disposition }; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head3 filename |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
$gif->filename; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Returns the content filename for use in HTTP headers. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub filename |
241
|
|
|
|
|
|
|
{ |
242
|
4
|
|
|
4
|
1
|
9
|
my ( $self, $arg ) = @_; |
243
|
4
|
50
|
|
|
|
12
|
$self->{ filename } = $arg if defined $arg; |
244
|
4
|
|
|
|
|
37
|
return $self->{ filename }; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head3 content |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
$gif->content; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Returns the image data to send in the HTTP response body. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub content |
256
|
|
|
|
|
|
|
{ |
257
|
2
|
|
|
2
|
1
|
4
|
my ( $self, $arg ) = @_; |
258
|
2
|
50
|
|
|
|
8
|
$self->{ content } = $arg if defined $arg; |
259
|
2
|
|
|
|
|
10
|
return $self->{ content }; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 TODO |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
mod_perl support |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 AUTHOR |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Rob Brown, C<< >> |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 BUGS |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
273
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you will |
274
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 SUPPORT |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
perldoc Image::Empty |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
You can also look for information at: |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=over 4 |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
L |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
L |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item * CPAN Ratings |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
L |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=item * Search CPAN |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
L |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=back |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
Copyright 2012 Rob Brown. |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
310
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
311
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=cut |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
1; |
318
|
|
|
|
|
|
|
|