[Templates] Image plugin (was: Modifying TT functions at run time)

darren chamberlain dlc@users.sourceforge.net
Tue, 5 Nov 2002 08:30:17 -0500


--Kj7319i9nmIyA2yE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

* Andy Wardley <abw@andywardley.com> [2002-11-04 13:56]:
> darren chamberlain wrote:
> >   package Template::Plugin::ImageSize;

[-- snip --]

> There might conceivably be other image related methods we want
> to add one day, so it makes sense to have one general purpose 
> image plugin.

As I looked into Image::Size, I noticed Gisle Aas' Image::Info module,
which gives all the same information as Image::Size, plus more:

  * media type (text/gif)
  * file extension (gif)
  * color type (Indexed-RGB)
  * comment ("Made with the GIMP!")
  * compression (which compression algo used)
  * interlace (1/0)
  * bits per pixel

And tons of other neat things for image types that support them -- to
see the kinds of image info available, run this in the module's build
directory:

  perl -MData::Dumper -MImage::Info=image_info -le 'print Dumper(image_info("test.jpg"))'

Note: test.jpg comes with the distribution, as part of the Image::Info test
suite.  You can run the above in the TT build directory too, of course: use
images/tt2power.gif or images/ttdotorg.gif instead of test.jpg.

Patches against lib/Template/Plugin/Image.pm and t/image.t attached; comments?

(darren)

-- 
Distrust any enterprise that requires new clothes.
    -- Henry David Thoreau

--Kj7319i9nmIyA2yE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="Image.patch"

Index: lib/Template/Plugin/Image.pm
===================================================================
RCS file: /template-toolkit/Template2/lib/Template/Plugin/Image.pm,v
retrieving revision 1.2
diff -u -d -w -u -p -r1.2 Image.pm
--- lib/Template/Plugin/Image.pm	2002/11/04 19:47:13	1.2
+++ lib/Template/Plugin/Image.pm	2002/11/05 13:28:57
@@ -22,12 +22,12 @@ package Template::Plugin::Image;
 require 5.004;
 
 use strict;
-use Image::Size;
+use Image::Info;
 use Template::Plugin;
 use File::Spec;
 use base qw( Template::Plugin );
 
-use vars qw( $VERSION );
+use vars qw( $VERSION $AUTOLOAD );
 
 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
 
@@ -71,66 +71,53 @@ sub new {
     }, $class;
 }
 
-
 #------------------------------------------------------------------------
-# size()
+# init()
 #
-# Return the width and height of the image as a 2 element list.
+# Calls image_info on $self->{ file }
 #------------------------------------------------------------------------
 
-sub size {
+sub init {
     my $self = shift;
-
-    # return cached size
-    return $self->{ size } if $self->{ size };
+    return $self if $self->{ size };
 
-    my ($width, $height, $error ) = imgsize($self->{ file });
-    return $self->throw($error) unless defined $width;
-    $self->{ width  } = $width;
-    $self->{ height } = $height;
-    return ($self->{ size } = [ $width, $height ]);
-}
+    my $image = Image::Info::image_info($self->{ file });
+    return $self->throw($image->{ error }) if defined $image->{ error };
 
+    @$self{ keys %$image } = values %$image;
+    $self->{ size } = [ $image->{ width }, $image->{ height } ];
 
-#------------------------------------------------------------------------
-# width()
-#
-# Return the width of the image.
-#------------------------------------------------------------------------
+    $self->{ lastmod } = (stat $self->{ file })[10];
 
-sub width {
-    my $self = shift;
-    $self->size() unless $self->{ size };
-    return $self->{ width };
+    return $self;
 }
 
-
 #------------------------------------------------------------------------
-# height()
+# attr()
 #
-# Return the height of the image.
+# Return the width and height as HTML/XML attributes.
 #------------------------------------------------------------------------
 
-sub height {
+sub attr {
     my $self = shift;
-    $self->size() unless $self->{ size };
-    return $self->{ height };
+    my $size = $self->size();
+    return "width=\"$size->[0]\" height=\"$size->[1]\"";
 }
 
-
 #------------------------------------------------------------------------
-# attr()
+# lastmod()
 #
-# Return the width and height as HTML/XML attributes.
+# Return last modification time as a time_t:
+#
+#   [% date.format(image.lastmod, "%Y/%m/%d") %]
 #------------------------------------------------------------------------
 
-sub attr {
+sub lastmod {
     my $self = shift;
-    my $size = $self->size();
-    return "width=\"$size->[0]\" height=\"$size->[1]\"";
+    $self->init;
+    return $self->{ lastmod };
 }
 
-
 #------------------------------------------------------------------------
 # tag(\%options)
 #
@@ -160,6 +147,14 @@ sub throw {
     die (Template::Exception->new('Image', $error));
 }
 
+sub AUTOLOAD {
+    my $self = shift;
+   (my $a = $AUTOLOAD) =~ s/.*:://;
+
+    $self->init;
+    return $self->{ $a };
+}
+
 1;
 
 __END__

--Kj7319i9nmIyA2yE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="Image-t.patch"

Index: t/image.t
===================================================================
RCS file: /template-toolkit/Template2/t/image.t,v
retrieving revision 1.1
diff -u -d -w -u -p -r1.1 image.t
--- t/image.t	2002/11/04 18:46:18	1.1
+++ t/image.t	2002/11/05 13:29:10
@@ -22,9 +22,9 @@ use Cwd;
 use File::Spec;
 $^W = 1;
 
-eval "use Image::Size";
+eval "use Image::Info";
 if ($@) {
-    skip_all('Image::Size not installed');
+    skip_all('Image::Info not installed');
 }
 
 my $dir  = -d 't' ? 'images' : File::Spec->catfile(File::Spec->updir(), 'images');

--Kj7319i9nmIyA2yE--