[SMARTY] Smarty and GD createimage

Nuno Mendes <[email protected]> Fri, 15 Dec 2006 15:59:22 +0000
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>
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

-- 
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php