For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > December 2006 > Re: [SMARTY] Smarty and GD createimage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [SMARTY] Smarty and GD createimage
Monte Ohrt

2006-12-15, 7:10 pm

Your second method is correct, there is no need to create an image using
a Smarty plugin. An image requires a separate request to the server to
display, it won't display "in-line" in the HTML.

Nuno Mendes wrote:
> Hi,
>
> I am having a problem trying to crate images on the fly using a smarty
> plugin for that porpose:
>
> I'm using 4 files:
> a) index.php (the main script)
> b) template.tpl (the template)
> c) createimage.php (a script to create an image)
> d) function.createimage.php (a smarty plugin to create an image)
>
> The code for them is:
>
> index.php:
>
> <?php
>
> require_once('lib/smarty/Smarty.class.php');
>
> $tpl = new Smarty;
> $tpl->template_dir = 'lib\tpl\templates';
> $tpl->config_dir = 'lib\tpl\config';
> $tpl->cache_dir = 'lib\tpl\cache';
> $tpl->compile_dir = 'lib\tpl\templates_c';
>
> $tpl->display('template.tpl');
>
> ?>
>
> template.tpl:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
> <body>
> This is a imagecreate test
> <hr>
> <img src="{createimage}" border="0">
> <hr>
> <img src="createimage.php" border="0">
> <hr>
> </body>
> </html>
>
> createimage.php:
>
> <?php
>
> $imgwidth = 800;
> $imgheight = 24;
>
> $im = imagecreate($imgwidth,$imgheight);
> $black = ImageColorAllocate($im, 000, 000, 000);
> $grey = ImageColorAllocate($im, 113, 111, 100);
> $white = ImageColorAllocate($im, 255, 255, 255);
>
> imagerectangle($im, 0, 0, $imgwidth, $imgheight, $grey);
> imagerectangle($im, 1, 1, $imgwidth-2, $imgheight-2, $white);
>
> header("Content-type: image/png");
> imagepng($im);
> imagedestroy($im);
>
> ?>
>
> function.createimage.php:
>
> <?php
>
> function smarty_function_createimage($params, &$smarty) {
>
> $imgwidth = 800;
> $imgheight = 24;
>
> $im = imagecreate($imgwidth,$imgheight);
> $black = ImageColorAllocate($im, 000, 000, 000);
> $grey = ImageColorAllocate($im, 113, 111, 100);
> $white = ImageColorAllocate($im, 255, 255, 255);
>
> imagerectangle($im, 0, 0, $imgwidth, $imgheight, $grey);
> imagerectangle($im, 1, 1, $imgwidth-2, $imgheight-2, $white);
>
> header("Content-type: image/png");
> imagepng($im);
> imagedestroy($im);
>
> }
>
> ?>
>
>
> As you can see in the template I am trying to output 2 images, 1
> created with the createimage plugin:
> <img src="{createimage}" border="0">
>
> and other created with the createimage.php script:
> <img src="createimage.php" border="0">
>
> and the createimage plugin and the createimage.php script have exactly
> the same code.
>
> The problem is that if I run this example I receive "The image
> “http://localhost/imgtest/index.php” cannot be displayed, because it
> contains errors".
>
> To avoid this error I have to comment the plugin line
> header("Content-type: image/png");
>
> and the script runs fine, but obviously the image generated with the
> plugin doesn't show.
>
> So what I would like to know is if there is a way of using a smarty
> plugin to generate an image on the fly.
>
> Thank you!
>
> Nuno Mendes
>

Max Schwanekamp

2006-12-15, 7:10 pm

Monte Ohrt wrote:
> Your second method is correct, there is no need to create an image using
> a Smarty plugin. An image requires a separate request to the server to
> display, it won't display "in-line" in the HTML.


What you would do is create a plugin to output an <img> tag with the
correct parameters to pass to your createimage.php script; or have the
plugin create the image, store it in a cache folder, and output the img
tag so the browser requests the just-created file.

This latter approach is taken by Cerdmann's Thumb plugin, at which you
might want to take a look.

http://www.cerdmann.com/thumb/

--
Max Schwanekamp
NeptuneWebworks.com
541-517-9064
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com