HTML::Template::Compiled::Plugin::InlineImage - Inline-Images with HTML::Template::Compiled
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.
$HTML::Template::Compiled::Plugin::InlineImage::SIZE_WARNING
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).
There are four escapy types at the moment:
renders as png
renders as gif
renders as jpeg
renders per default as png, or if you feed it raw data, it will guess the image type.
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 =)
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.
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
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)
EXAMPLES
Simple examples:
"gd.pl"" in "examples and "gd.html"" in "examples
"raw.pl"" in "examples and "raw.html"" in "examples
HTML::Template::Compiled
Copyright (C) 2006 by tina mueller
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.
To install HTML::Template::Compiled::Plugin::InlineImage, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Template::Compiled::Plugin::InlineImage
CPAN shell
perl -MCPAN -e shell install HTML::Template::Compiled::Plugin::InlineImage
For more information on module installation, please visit the detailed CPAN module installation guide.