HTML::Template::Compiled::Plugin::InlineImage - Inline-Images with HTML::Template::Compiled


HTML-Template-Compiled-Plugin-InlineImage documentation Contained in the HTML-Template-Compiled-Plugin-InlineImage distribution.

Index


Code Index:

NAME

Top

HTML::Template::Compiled::Plugin::InlineImage - Inline-Images with HTML::Template::Compiled

SYNOPSIS

Top

The Perl code:

    use HTML::Template::Compiled::Plugin::InlineImage;

    my $htc = HTML::Template::Compiled->new(
        plugin => [qw(HTML::Template::Compiled::Plugin::InlineImage)],
        filename => "template.htc",
        tagstyle => [qw(+tt)],
    );
    $htc->param(gd_object => $gd);
    $htc->param(raw_data => $data_from_image_file);
    print $htc->output;

The Template:

    <html>
        <body>
        <img [%= gd_object escape="INLINE_IMG" %] alt="[Rendered GD Image]">
        <img [%= raw_data escape="INLINE_IMG" %] alt="[Rendered Image]">
        </body>
    </html>

This will create an inline image. The GD-object/the raw image output is turned into base64 and put into a src attribute.

The output looks like

    src="data:image/type;base64,...." width="42" height="42"

Note that the maximum length for a HTML src attribute is 1024. If your image is bigger you will get a warning.

To avoid the warning, set $HTML::Template::Compiled::Plugin::InlineImage::SIZE_WARNING to 0.

DESCRIPTION

Top

This is a plugin for HTML::Template::Compiled. If you feed it GD-objects or raw images (other image-object-types could be added in the future), then it will render the object like described in RFC 2397 (http://www.ietf.org/rfc/rfc2397.txt).

ESCAPE TYPES

Top

There are four escapy types at the moment:

INLINE_IMG_PNG

renders as png

INLINE_IMG_GIF

renders as gif

INLINE_IMG_JPEG

renders as jpeg

INLINE_IMG

renders per default as png, or if you feed it raw data, it will guess the image type.

METHODS

Top

register

Gets called by HTC. It should return a hashref. I will document soon in HTML::Template::Compiled what this method should return to create a plugin. Until then, have a lok at the source =)

SUBROUTINES

Top

inline

Arguments: a hash like

    type  => 'png', # or jpeg, gif
    image => $gd_object,

The subroutine determines which kind of image object we have and calls the corresponding *_to_binary method. (At the moment only GD is supported.)

This function is usually only used internally.

gd_to_binary

Arguments: ($gd_object, $type)

$type can be png, jpeg or gif.

Returns:

    src="data:image/$type;base64,$the_rendered_image_as_base64",
    width of image,
    heigth of image

This function is usually only used internally.

ERROR MESSAGES, WARNINGS

Top

If your image is (in base64, plus mime type) bigger than 1024 bytes, you'll get a warning like:

    Image is too big (1234 characters > 1024)

To avoid the warning, set $HTML::Template::Compiled::Plugin::InlineImage::SIZE_WARNING to 0.

EXAMPLES

Simple examples:

"examples/gd.pl" and "examples/gd.html"

"examples/raw.pl" and "examples/raw.html"

SEE ALSO

Top

HTML::Template::Compiled

COPYRIGHT AND LICENSE

Top


HTML-Template-Compiled-Plugin-InlineImage documentation Contained in the HTML-Template-Compiled-Plugin-InlineImage distribution.

package HTML::Template::Compiled::Plugin::InlineImage;
# $Id: InlineImage.pm,v 1.14 2006/09/14 10:28:35 tinita Exp $
use strict;
use warnings;
use Carp qw(croak carp);
use HTML::Template::Compiled::Expression qw(:expressions);
use HTML::Template::Compiled;
use MIME::Base64;
our $VERSION = '0.03';
HTML::Template::Compiled->register(__PACKAGE__);
our $SIZE_WARNING = 1;


sub register {
    my ($class) = @_;
    my %plugs = (
        escape => {
            # <img <%= gd_object escape="INLINE_IMG"%> alt="blah">
            INLINE_IMG => sub {
                HTML::Template::Compiled::Plugin::InlineImage::inline(
                    type => 'png',
                    image => $_[0],
                );
            },
            INLINE_IMG_PNG => sub {
                HTML::Template::Compiled::Plugin::InlineImage::inline(
                    type => 'png',
                    image => $_[0],
                );
            },
            INLINE_IMG_GIF => sub {
                HTML::Template::Compiled::Plugin::InlineImage::inline(
                    type => 'gif',
                    image => $_[0],
                );
            },
            INLINE_IMG_JPEG => sub {
                HTML::Template::Compiled::Plugin::InlineImage::inline(
                    type => 'jpeg',
                    image => $_[0],
                );
            },
        },
    );
    return \%plugs;
}

sub inline {
    my (%args) = @_;
    my $image = $args{image};
    my $type = $args{type};
    my ($binary, $width, $height);
    unless (ref $image) {
        # we have raw data, try guessing mime type
        require File::MMagic;
        my $mm = File::MMagic->new;
        my $mtype = $mm->checktype_contents($image)
            or croak "Could not determine mime type";
        my ($type_a,$type_b) = split m#/#, $mtype;
        $type = $type_b;
        $binary = $image;
    }
    else {
        ($binary, $width, $height) = ref $image eq 'GD::Image'
            ? gd_to_binary($image,$type)
            : croak "unknown image type " . ref $image;
    }
    my $base64 = encode_base64($binary);
    my $string = "data:image/$type;base64,$base64";
    my $l = length $string;
    if ($l > 1024 && $SIZE_WARNING) {
        carp "Image is too big ($l characters > 1024)";
    }
    my $attributes = qq{src="$string"};
    if (defined $width) { $attributes .= qq{ width="$width"} }
    if (defined $height) { $attributes .= qq{ height="$height"} }
    return $attributes;
}

sub gd_to_binary {
    my $binary;
    if ($_[1] eq 'png') { $binary = $_[0]->png }
    if ($_[1] eq 'gif') { $binary = $_[0]->gif }
    if ($_[1] eq 'jpeg') { $binary = $_[0]->jpeg }
    my ($width,$height) = $_[0]->getBounds();
    return ($binary, $width, $height);
}

1;

__END__