I am using the following code to generate a dynamic image which works fine when you view in a browser using image.php?text=hello world. The problem lies when i try to use it within a page as an image (see second code box).
I cannot pass the $_GET through via the url in this case because of the nature of the site and use of rewritten urls.
What would be the best method to pass the text for each page through. I did think of using sessions but this would be a bit overkill to produce a product image heading.
<?php
// Set the content-type as a PNG image (allows transparency)
header('Content-type: image/png');
// The text to draw into the image
$text = strtoupper($_GET['text']);
// The name of the font you want to use (this must be in the same directory as your file unless you specify a different directory
$font = 'HelveticaNeueLight.ttf';
$size = 20;
$width = strlen($text) * $size;
$height = $size + 10;
// Create the image 450px wide by 30 pixels high
$im = imagecreatetruecolor($width, $height);
imagealphablending($im, true);
// Create the text colour with alpha transparency
$alpha = imagecolorallocatealpha($im, 255, 255, 255, 127);
$black = imagecolorallocate($im, 0, 0, 0);
//fill the background with the transparant colour
imagefill($im, 0, 0, $alpha);
// Add the text using the $red colour
imagettftext($im, $size, 0, 10, 20, $black, $font, $text);
// Add a shadow using the $black colour
//imagettftext($im, 20, 0, 11, 21, $black, $font, $text);
//save the alpha transparency
imagesavealpha($im, true);
// create the PNG
imagepng($im);
//destroy the $im from the memory
imagedestroy($im);
?>
Using something along the lines of this would be perfect but I cant think how I would obtain the value of ?text=
<img src="image.php?text=hello world" alt="hello world"/>