I'd like some feedback on a plugin I threw together today.  It allows
you to generate thumbnail images from within a template.  I've used
the concept before in an older project, but thought it would be nice
to wrap it up in a plugin.

Effectively, it extends the Template::Plugin::Image plugin to allow
you to specify a width and height parameter, and the plugin will
automatically generate a thumbnail with these dimensions, and save it
on the filesystem.  The return value is exactly what you get from
Template::Plugin::Image, except that it gives the location and details
of the thumbnail, instead of the original image.

The reason I like this approach is because it moves another design
decision to the template, instead of cluttering up the perl code.  The
size of a thumbnail is going to be decided by the template designer,
so (s)he should have the power to make those changes without requiring
a programmer.

Performance should be reasonable.  Since thumbnails are stored on the
filesystem after they are generated, you only get hit with the
thumbnail generation on the first request.  Afterwards the only delay
will be a couple of stat calls to the original image and the thumbnail
image.

You can find the plugin here:

http://cees.crtconsulting.ca/perl/modules/Template-Plugin-Image-Thumbnail-0.01.tar.gz

Or just browse the tarball here:

http://cees.crtconsulting.ca/perl/modules/Template-Plugin-Image-Thumbnail-0.01/


I will upload to CPAN if this is deemed useful, and there are no
issues with the name.  Since I just wrote it today, I have not had
time to test it extensively (besides what is covered in the test
suite), but I will start using it in my code right away... 
Suggestions and patches welcome.

Docs are included below:

NAME
    Template::Plugin::Image::Thumbnail - Plugin access to generate image
    thumbnails

SYNOPSIS
        [% USE image = Image::Thumbnail(filename width=100 height=100) %]
        [% image.name %]
        [% image.tag %]

        [% USE image = Image::Thumbnail(root='/var/www'
name='/images/test.jpg' width=100) %]
        [% image.name %]
        [% image.tag %]

DESCRIPTION
    This plugin is an extension of Template::Plugin::Image and allows for
    easy creation of thumbnails images through the
    Image::Magick::Thumbnail::Fixed module.

    This plugin adds four more parameters that can be used to decide how to
    create the thumbnail. For all the other available options please see the
    docs for Template::Plugin::Image.

    If you do not provide either a width or height parameter, then this
    plugin will work exactly like the Template::Plugin::Image module.

OPTIONS
    width
        This will specify the width of the thumbnail. If this option is
        omitted, thumbnail will be scaled appropriately based on the height
        of the thumbnail so that the height to width ratio of the original
        image is maintained.

    height
        This will specify the height of the thumbnail. If this option is
        omitted, thumbnail will be scaled appropriately based on the width
        of the thumbnail so that the height to width ratio of the original
        image is maintained.

    bgcolor
        If the width and height are both specified, and the width to height
        ratio is not the same as the original image, then the image will be
        padded either on the top and bottom or the sides. The color of the
        padding can be specified using the bgcolor option. See the docs for
        Image::Magick::Thumbnail::Fixed for more information on this option.

    gravity
        If the width and height are both specified, and the width to height
        ratio is not the same as the original image, then the image will be
        padded either on the top, bottom or sides. The gravity option will
        determine where the image will be positioned in this new image. By
        default, the image is centered. See the docs for
        Image::Magick::Thumbnail::Fixed for more information on this option.

AUTHOR
    Cees Hek <[EMAIL PROTECTED]>

COPYRIGHT
      Copyright (C) 2005 Cees Hek.  All Rights Reserved.

    This module is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

SEE ALSO
    Template::Plugin, Template::Plugin::Image,
    Image::Magick::Thumbnail::Fixed

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to