| 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.106'; | ||
| 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 }; | ||
| 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 = $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 } //= ''; | ||
| 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 })[9]; | ||
| 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 | |||
| 167 | if (%$options) { | ||
| 168 | for my $key (sort keys %$options) { | ||
| 169 | my $escaped = escape( $options->{$key} ); | ||
| 170 | $tag .= qq[ $key="$escaped"]; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | $tag .= ' />'; | ||
| 175 | |||
| 176 | return $tag; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub escape { | ||
| 180 | my ($text) = @_; | ||
| 181 | for ($text) { | ||
| 182 | s/&/&/g; | ||
| 183 | s/</</g; | ||
| 184 | s/>/>/g; | ||
| 185 | s/"/"/g; | ||
| 186 | } | ||
| 187 | $text; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub throw { | ||
| 191 | my ($self, $error) = @_; | ||
| 192 | die (Template::Exception->new('Image', $error)); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub AUTOLOAD { | ||
| 196 | my $self = shift; | ||
| 197 | (my $a = $AUTOLOAD) =~ s/.*:://; | ||
| 198 | |||
| 199 | $self->init; | ||
| 200 | return $self->{ $a }; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ |