<?php
$color = $_GET["colour"];
$text = strtolower(urldecode(stripslashes($_GET["text"])));
$bg = $_GET["bg"];
$font = "/tiza.ttf";
//Set the image width and height
if(empty($_GET["width"]) && empty($_GET["height"]))
{
$width = 400;
$height = 40;
$size = "22";
}
else
{
$width = $_GET["width"];
$height = $_GET["height"];
$size = "18";
}
//Create the image resource
$image = imagecreate($width, $height);
function ImageColorAllocateFromHex ($img, $hexstr)
{
$int = hexdec($hexstr);
return imagecolorallocate ($img,
0xFF & ($int >> 0x10),
0xFF & ($int >> 0x8),
0xFF & $int);
}
$rgb = ImageColorAllocateFromHex($image, $_GET["bg"]);
$textCol = ImageColorAllocateFromHex($image, $color);
imagefill($image, 0, 0, $rgb);
//Add randomly generated string in white to the image
imagettftext($image,$size,0,0,30,$textCol, $font, $text);
//Tell the browser what kind of file is come in
header("Content-Type: image/gif");
//Output the newly created image in jpeg format
imagegif($image);
//Free up resources
imagedestroy($image);
?>
Hi dannywade,
In the above code, I have noticed that you have no fail safe. Meaning, if the user specifies the incorrect size image, the text may not fit on. Is this intentional?
Personally, I would find a way to measure the length of the text in pixels to ensure it fits into the specified image and then widen (if that's even a word) the image to fit the text.
I can see the benefits of this for captcha images etc.
Good work.