File Coverage

blib/lib/Rose/HTML/Image.pm
Criterion Covered Total %
statement 47 61 77.0
branch 10 18 55.5
condition 5 16 31.2
subroutine 15 18 83.3
pod 8 9 88.8
total 85 122 69.6


line stmt bran cond sub pod time code
1              
2             use strict;
3 3     3   6714  
  3         6  
  3         79  
4             use Image::Size;
5 3     3   968  
  3         8507  
  3         148  
6             use base 'Rose::HTML::Object';
7 3     3   17  
  3         6  
  3         871  
8             our $DOC_ROOT;
9              
10             our $VERSION = '0.606';
11              
12             __PACKAGE__->add_required_html_attrs(
13             {
14             'alt', => '',
15             'src', => '',
16             });
17              
18              
19             __PACKAGE__->add_valid_html_attrs
20             (
21             'src', # %URI; #REQUIRED -- URI of image to embed --
22             'alt', # %Text; #REQUIRED -- short description --
23             'longdesc', # %URI; #IMPLIED -- link to long description --
24             'name', # CDATA #IMPLIED -- name of image for scripting --
25             'height', # %Length; #IMPLIED -- override height --
26             'width', # %Length; #IMPLIED -- override width --
27             'usemap', # %URI; #IMPLIED -- use client-side image map --
28             'ismap', # (ismap) #IMPLIED -- use server-side image map --
29             );
30              
31             __PACKAGE__->add_boolean_html_attrs
32             (
33             'ismap',
34             );
35              
36              
37 12     12 1 45  
38             QUIET:
39 0     0 1 0 {
40 6     6 1 38 no warnings 'uninitialized';
41 6     6 1 23 use constant MOD_PERL_1 => ($ENV{'MOD_PERL'} && !$ENV{'MOD_PERL_API_VERSION'}) ? 1 : 0;
42             use constant MOD_PERL_2 => ($ENV{'MOD_PERL'} && $ENV{'MOD_PERL_API_VERSION'} == 2) ? 1 : 0;
43              
44             use constant TRY_MOD_PERL_2 => eval { require Apache2::RequestUtil } && !$@ ? 1 : 0;
45 3     3   30 }
  3         6  
  3         176  
46 3 50 33 3   19  
  3         6  
  3         284  
47 3 50 33 3   19 {
  3         6  
  3         236  
48             if(MOD_PERL_1)
49 3 50 33 3   25 {
  3         5  
  3         5  
50             return Apache->request->document_root;
51             }
52              
53             if(TRY_MOD_PERL_2)
54 4     4 0 6 {
55             my $r;
56              
57             TRY:
58             {
59 4         4 local $@;
60             eval { $r = Apache2::RequestUtil->request };
61             }
62              
63             if($r)
64             {
65             return $r->document_root;
66             }
67             }
68              
69             return $DOC_ROOT || '';
70             }
71              
72             {
73             my($self) = shift;
74             my $src = $self->html_attr('src', @_);
75 4   50     26 $self->_new_src_or_document_root($src) if(@_);
76             return $src;
77             }
78              
79             {
80 8     8 1 40 my($self) = shift;
81 8         20 return $self->{'path'} unless(@_);
82 8 100       24 $self->_new_path($self->{'path'} = shift);
83 8         35 return $self->{'path'};
84             }
85              
86             {
87             my($self) = shift;
88 0     0 1 0  
89 0 0       0 if(@_)
90 0         0 {
91 0         0 $self->{'document_root'} = shift;
92             $self->_new_src_or_document_root($self->src);
93             return $self->{'document_root'};
94             }
95              
96 11     11 1 28 $self->{'document_root'} = $self->init_document_root
97             unless(defined $self->{'document_root'});
98 11 100       22  
99             return $self->{'document_root'};
100 3         5 }
101 3         5  
102 3         7 {
103             my($self, $src) = @_;
104              
105             if(-e $src)
106 8 100       35 {
107             $self->{'path'} = $src;
108 8         32 }
109             else
110             {
111             $self->{'path'} = $self->document_root . $src;
112             }
113 8     8   13  
114             $self->init_size($self->{'path'});
115 8 50       151 }
116              
117 0         0 {
118             my($self, $path) = @_;
119              
120             unless($self->{'document_root'})
121 8         24 {
122             $self->init_size;
123             return;
124 8         20 }
125              
126             my $src = $path;
127              
128             $src =~ s/^$self->{'document_root'}//;
129 0     0   0  
130             $self->html_attr('src' => $src);
131 0 0       0  
132             $self->init_size;
133 0         0 }
134 0         0  
135             {
136             my($self, $path) = @_;
137 0         0  
138             $path ||= $self->{'path'} || return;
139 0         0  
140             my($w, $h) = Image::Size::imgsize($path);
141 0         0  
142             $self->html_attr(width => $w);
143 0         0 $self->html_attr(height => $h);
144             }
145              
146             1;
147              
148 8     8 1 15  
149             =head1 NAME
150 8   0     16  
      33        
151             Rose::HTML::Image - Object representation of the "img" HTML tag.
152 8         29  
153             =head1 SYNOPSIS
154 8         24283  
155 8         28 $img = Rose::HTML::Image->new(src => '/logo.png',
156             alt => 'Logo');
157              
158             $img->document_root('/var/web/htdocs');
159              
160             # <img alt="Logo" height="48" src="/logo.png" width="72">
161             print $img->html;
162              
163             $img->alt(undef);
164              
165             # <img alt="" height="48" src="/logo.png" width="72" />
166             print $img->xhtml;
167              
168             ...
169              
170             =head1 DESCRIPTION
171              
172             L<Rose::HTML::Image> is an object representation of the E<lt>imgE<gt> HTML tag. It includes the ability to automatically fill in the "width" and "height" HTML attributes with the correct values, provided it is given enough information to find the actual image file on disk. The L<Image::Size> module is used to read the file and determine the correct dimensions.
173              
174             This class inherits from, and follows the conventions of, L<Rose::HTML::Object>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Object> documentation for more information.
175              
176             =head1 HTML ATTRIBUTES
177              
178             Valid attributes:
179              
180             alt
181             class
182             dir
183             height
184             id
185             ismap
186             lang
187             longdesc
188             name
189             onclick
190             ondblclick
191             onkeydown
192             onkeypress
193             onkeyup
194             onmousedown
195             onmousemove
196             onmouseout
197             onmouseover
198             onmouseup
199             src
200             style
201             title
202             usemap
203             width
204             xml:lang
205              
206             Required attributes:
207              
208             alt
209             src
210              
211             Boolean attributes:
212              
213             ismap
214              
215             =head1 CONSTRUCTOR
216              
217             =over 4
218              
219             =item B<new PARAMS>
220              
221             Constructs a new L<Rose::HTML::Image> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.
222              
223             =back
224              
225             =head1 OBJECT METHODS
226              
227             =over 4
228              
229             =item B<document_root [PATH]>
230              
231             Get or set the web site document root. This is combined with the value of the "src" HTML attribute to build the path to the actual image file on disk.
232              
233             If running in a mod_perl 1.x environment, the document root defaults to the value returned by:
234              
235             Apache->request->document_root
236              
237             If running in a mod_perl 2.x environment, the document root defaults to the value returned by:
238              
239             Apache2::RequestUtil->request->document_root
240              
241             Note that you must have the C<GlobalRequest> option set for this to work. If you do not, the document root defaults to undef.
242              
243             These calls are made once for each L<Rose::HTML::Image> object that needs to use the document root.
244              
245             =item B<init_size [PATH]>
246              
247             Try to set the "width" and "height" HTML attributes but using L<Image::Size> to read the image file on disk. If a PATH argument is passed, the image file is read at that location. Otherwise, if the L<path()|/path> attribute is set, that path is used. Failing that, the width and height HTML attributes are simply not modified.
248              
249             =item B<path [PATH]>
250              
251             Get or set the path to the image file on disk.
252              
253             If a PATH argument is passed and L<document_root()|/document_root> is defined, then PATH has L<document_root()|/document_root> removed from the front of it (substitution anchored at the start of PATH) and the resulting string is set as the value of the "src" HTML attribute. Regardless of the value of L<document_root()|/document_root>, L<init_size()|/init_size> is called in an attempt to set the "height" and "width" HTML attributes.
254              
255             The current value of the C<path> object attribute is returned.
256              
257             =item B<src [SRC]>
258              
259             Get or set the value of the "src" HTML attribute.
260              
261             If a SRC argument is passed and a file is found at the path specified by SRC, then L<path()|/path> is set to SRC. Otherwise, L<path()|/path> is set to the concatenation of L<document_root()|/document_root> and SRC. In either case, L<init_size()|/init_size> is called in an attempt to set the "height" and "width" HTML attributes.
262              
263             The current value of the "src" HTML attribute is returned.
264              
265             =back
266              
267             =head1 AUTHOR
268              
269             John C. Siracusa (siracusa@gmail.com)
270              
271             =head1 LICENSE
272              
273             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.