line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Template::Compiled::Plugin::InlineImage; |
2
|
|
|
|
|
|
|
# $Id: InlineImage.pm,v 1.14 2006/09/14 10:28:35 tinita Exp $ |
3
|
1
|
|
|
1
|
|
113254
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp qw(croak carp); |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
71
|
|
6
|
1
|
|
|
1
|
|
5
|
use HTML::Template::Compiled::Expression qw(:expressions); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
523
|
|
7
|
1
|
|
|
1
|
|
8
|
use HTML::Template::Compiled; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
33
|
use MIME::Base64; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
661
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
10
|
|
|
|
|
|
|
HTML::Template::Compiled->register(__PACKAGE__); |
11
|
|
|
|
|
|
|
our $SIZE_WARNING = 1; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
15
|
1
|
|
|
1
|
1
|
13
|
my ($class) = @_; |
16
|
|
|
|
|
|
|
my %plugs = ( |
17
|
|
|
|
|
|
|
escape => { |
18
|
|
|
|
|
|
|
# alt="blah"> |
19
|
|
|
|
|
|
|
INLINE_IMG => sub { |
20
|
0
|
|
|
0
|
|
0
|
HTML::Template::Compiled::Plugin::InlineImage::inline( |
21
|
|
|
|
|
|
|
type => 'png', |
22
|
|
|
|
|
|
|
image => $_[0], |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
INLINE_IMG_PNG => sub { |
26
|
1
|
|
|
1
|
|
2792
|
HTML::Template::Compiled::Plugin::InlineImage::inline( |
27
|
|
|
|
|
|
|
type => 'png', |
28
|
|
|
|
|
|
|
image => $_[0], |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
INLINE_IMG_GIF => sub { |
32
|
0
|
|
|
0
|
|
0
|
HTML::Template::Compiled::Plugin::InlineImage::inline( |
33
|
|
|
|
|
|
|
type => 'gif', |
34
|
|
|
|
|
|
|
image => $_[0], |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
INLINE_IMG_JPEG => sub { |
38
|
0
|
|
|
0
|
|
0
|
HTML::Template::Compiled::Plugin::InlineImage::inline( |
39
|
|
|
|
|
|
|
type => 'jpeg', |
40
|
|
|
|
|
|
|
image => $_[0], |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
}, |
44
|
1
|
|
|
|
|
13
|
); |
45
|
1
|
|
|
|
|
4
|
return \%plugs; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub inline { |
49
|
1
|
|
|
1
|
1
|
4
|
my (%args) = @_; |
50
|
1
|
|
|
|
|
3
|
my $image = $args{image}; |
51
|
1
|
|
|
|
|
2
|
my $type = $args{type}; |
52
|
1
|
|
|
|
|
1
|
my ($binary, $width, $height); |
53
|
1
|
50
|
|
|
|
5
|
unless (ref $image) { |
54
|
|
|
|
|
|
|
# we have raw data, try guessing mime type |
55
|
0
|
|
|
|
|
0
|
require File::MMagic; |
56
|
0
|
|
|
|
|
0
|
my $mm = File::MMagic->new; |
57
|
0
|
0
|
|
|
|
0
|
my $mtype = $mm->checktype_contents($image) |
58
|
|
|
|
|
|
|
or croak "Could not determine mime type"; |
59
|
0
|
|
|
|
|
0
|
my ($type_a,$type_b) = split m#/#, $mtype; |
60
|
0
|
|
|
|
|
0
|
$type = $type_b; |
61
|
0
|
|
|
|
|
0
|
$binary = $image; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else { |
64
|
1
|
50
|
|
|
|
4
|
($binary, $width, $height) = ref $image eq 'GD::Image' |
65
|
|
|
|
|
|
|
? gd_to_binary($image,$type) |
66
|
|
|
|
|
|
|
: croak "unknown image type " . ref $image; |
67
|
|
|
|
|
|
|
} |
68
|
1
|
|
|
|
|
4
|
my $base64 = encode_base64($binary); |
69
|
1
|
|
|
|
|
3
|
my $string = "data:image/$type;base64,$base64"; |
70
|
1
|
|
|
|
|
3
|
my $l = length $string; |
71
|
1
|
0
|
33
|
|
|
3
|
if ($l > 1024 && $SIZE_WARNING) { |
72
|
0
|
|
|
|
|
0
|
carp "Image is too big ($l characters > 1024)"; |
73
|
|
|
|
|
|
|
} |
74
|
1
|
|
|
|
|
3
|
my $attributes = qq{src="$string"}; |
75
|
1
|
50
|
|
|
|
2
|
if (defined $width) { $attributes .= qq{ width="$width"} } |
|
1
|
|
|
|
|
3
|
|
76
|
1
|
50
|
|
|
|
3
|
if (defined $height) { $attributes .= qq{ height="$height"} } |
|
1
|
|
|
|
|
2
|
|
77
|
1
|
|
|
|
|
24
|
return $attributes; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub gd_to_binary { |
81
|
1
|
|
|
1
|
1
|
2
|
my $binary; |
82
|
1
|
50
|
|
|
|
4
|
if ($_[1] eq 'png') { $binary = $_[0]->png } |
|
1
|
|
|
|
|
5
|
|
83
|
1
|
50
|
|
|
|
7
|
if ($_[1] eq 'gif') { $binary = $_[0]->gif } |
|
0
|
|
|
|
|
0
|
|
84
|
1
|
50
|
|
|
|
3
|
if ($_[1] eq 'jpeg') { $binary = $_[0]->jpeg } |
|
0
|
|
|
|
|
0
|
|
85
|
1
|
|
|
|
|
4
|
my ($width,$height) = $_[0]->getBounds(); |
86
|
1
|
|
|
|
|
6
|
return ($binary, $width, $height); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__END__ |