Generating an image from text
if(!isset($_POST['text_value']))
{
$_POST['text_value'] = "This is an image displaying text from the input box";
}
?>
Text to display:
How did I do that?
Generating an image from text can be easy with PHP and GD. This requires GD 1.8 or higher.
Check out www.php.net for an exact description of the php functions used in this code. They are all pretty much built in GD functions.
Now the part that make this code great is that there is no temporary files saved on the server, it is all done in memory.
Save this code as generate_image.php
<?php
function trimLength($data,$len)
{
if(strlen($data)>$len)
{
$data = substr($data,0,$len);
}
return $data;
}
function filterText($data)
{
return preg_replace("/[^A-Za-z0-9.,\s\s+]/","",$data);
}
$text = filterText($_GET['text']); // remove all illegal characters
$text = trimLength($text, 60); // trim to sixty characters
if($text == "") { $text = "Text"; }
$font = 4;
$width = ImageFontWidth($font) * strlen($text);
$height = ImageFontHeight($font);
header("Content-type: image/gif;");
$im = @imagecreatetruecolor($width, $height)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 0, 0, 0);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255) ;
imagefilledrectangle($im,0,0,$width,$height,$COULEUR_BLANC) ;
imagestring($im, $font, 0, 0, $text, $text_color);
imagegif($im);
imagedestroy($im);
?>
And then on the page you want the image created add this code



