blib/lib/Template/Plugin/Image.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 145 CC 16 SCAR 32.5
CC
16
Cov
0%
new
72.0
BEGIN
20.0
tag
20.0
CRAP 25.7
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Image
4 #
5 # DESCRIPTION
6 # Plugin for encapsulating information about an image.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # This module is free software; you can redistribute it and/or
13 # modify it under the same terms as Perl itself.
14 #
15 #============================================================================
16  
17 package Template::Plugin::Image;
18  
19 use strict;
20 use warnings;
21 use base 'Template::Plugin';
22 use Template::Exception;
23 use File::Spec;
24  
25 our $VERSION = '3.100';
26 our $AUTOLOAD;
27  
28 BEGIN {
29 if (eval { require Image::Info; }) {
30 *img_info = \&Image::Info::image_info;
31 }
32 elsif (eval { require Image::Size; }) {
33 *img_info = sub {
34 my $file = shift;
35 my @stuff = Image::Size::imgsize($file);
36 return {
37 "width" => $stuff[0],
38 "height" => $stuff[1],
39 "error" =>
40 # imgsize returns either a three letter file type
41 # or an error message as third value
42 (defined($stuff[2]) && length($stuff[2]) > 3
43 ? $stuff[2]
44 : undef),
45 };
46 }
47 }
48 else {
49 die(Template::Exception->new("image",
50 "Couldn't load Image::Info or Image::Size: $@"));
51 }
52  
53 }
54  
55 #------------------------------------------------------------------------
56 # new($context, $name, \%config)
57 #
58 # Create a new Image object. Takes the pathname of the file as
59 # the argument following the context and an optional
60 # hash reference of configuration parameters.
61 #------------------------------------------------------------------------
62  
63 sub new {
64 my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { };
65 my ($class, $context, $name) = @_;
66 my ($root, $file, $type);
67  
68 # name can be a positional or named argument
69 $name = $config->{ name } unless defined $name;
70  
71 return $class->throw('no image file specified')
72 unless defined $name and length $name;
73  
74 # name can be specified as an absolute path or relative
75 # to a root directory
76  
77 if ($root = $config->{ root }) {
78 $file = File::Spec->catfile($root, $name);
79 }
80 else {
81 $file = defined $config->{file} ? $config->{file} : $name;
82 }
83  
84 # Make a note of whether we are using Image::Size or
85 # Image::Info -- at least for the test suite
86 $type = $INC{"Image/Size.pm"} ? "Image::Size" : "Image::Info";
87  
88 # set a default (empty) alt attribute for tag()
89 $config->{ alt } = '' unless defined $config->{ alt };
90  
91 # do we want to check to see if file exists?
92 bless {
93 %$config,
94 name => $name,
95 file => $file,
96 root => $root,
97 type => $type,
98 }, $class;
99 }
100  
101 #------------------------------------------------------------------------
102 # init()
103 #
104 # Calls image_info on $self->{ file }
105 #------------------------------------------------------------------------
106  
107 sub init {
108 my $self = shift;
109 return $self if $self->{ size };
110  
111 my $image = img_info($self->{ file });
112 return $self->throw($image->{ error }) if defined $image->{ error };
113  
114 @$self{ keys %$image } = values %$image;
115 $self->{ size } = [ $image->{ width }, $image->{ height } ];
116  
117 $self->{ modtime } = (stat $self->{ file })[10];
118  
119 return $self;
120 }
121  
122 #------------------------------------------------------------------------
123 # attr()
124 #
125 # Return the width and height as HTML/XML attributes.
126 #------------------------------------------------------------------------
127  
128 sub attr {
129 my $self = shift;
130 my $size = $self->size();
131 return "width=\"$size->[0]\" height=\"$size->[1]\"";
132 }
133  
134  
135 #------------------------------------------------------------------------
136 # modtime()
137 #
138 # Return last modification time as a time_t:
139 #
140 # [% date.format(image.modtime, "%Y/%m/%d") %]
141 #------------------------------------------------------------------------
142  
143 sub modtime {
144 my $self = shift;
145 $self->init;
146 return $self->{ modtime };
147 }
148  
149  
150 #------------------------------------------------------------------------
151 # tag(\%options)
152 #
153 # Return an XHTML img tag.
154 #------------------------------------------------------------------------
155  
156 sub tag {
157 my $self = shift;
158 my $options = ref $_[0] eq 'HASH' ? shift : { @_ };
159  
160 my $tag = '<img src="' . $self->name() . '" ' . $self->attr();
161  
162 # XHTML spec says that the alt attribute is mandatory, so who
163 # are we to argue?
164  
165 $options->{ alt } = $self->{ alt }
166 unless defined $options->{ alt };
167  
168 if (%$options) {
169 for my $key (sort keys %$options) {
170 my $escaped = escape( $options->{$key} );
171 $tag .= qq[ $key="$escaped"];
172 }
173 }
174  
175 $tag .= ' />';
176  
177 return $tag;
178 }
179  
180 sub escape {
181 my ($text) = @_;
182 for ($text) {
183 s/&/&amp;/g;
184 s/</&lt;/g;
185 s/>/&gt;/g;
186 s/"/&quot;/g;
187 }
188 $text;
189 }
190  
191 sub throw {
192 my ($self, $error) = @_;
193 die (Template::Exception->new('Image', $error));
194 }
195  
196 sub AUTOLOAD {
197 my $self = shift;
198 (my $a = $AUTOLOAD) =~ s/.*:://;
199  
200 $self->init;
201 return $self->{ $a };
202 }
203  
204 1;
205  
206 __END__