Click to See Complete Forum and Search --> : PHP5 Image title generator


dannywade
03-12-2008, 08:07 AM
hi there,

I've recently put a wordpress together just to use a blog really and I've decided to post scripts up on it. I've created an image title creator using PHP5 that works in PHP4, so you can create nice looking titles using your own custom fonts without the user having them on their machine.

If you could take a look here, PHP5 Image Title Generation (http://dannywade.com/2008/03/12/php5-image-title-generation/) then i'd appreciate any feedback, good or bad! :)

iceomnia
03-12-2008, 08:35 AM
<?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.

laserlight
03-12-2008, 08:36 AM
A few initial comments:

1. $_GET["colour"], $_GET["text"] and $_GET["bg"] should be checked with isset or empty before being used.

2. stripslashes() should not be used as magic_quotes_gpc should be set to Off. If you do want to use it, use it only if get_magic_quotes_gpc() returns true.

3. There is no need to use urldecode().

4. If the width is specified but the height is not specified, there will still be an attempt to assign $_GET["height"] to $height. I suggest assigning them separately.

dannywade
03-12-2008, 08:38 AM
Hi,

I can see where your coming from and yes i think it would be a good idea.

The only reason it does this, is i created it for a project and the title size was fixed :) and it was also for a Captcha, so hardcoded 100x100 image for example. I'm sure i can find a fix for it no problem though!

Thanks for reply :)

dannywade
03-12-2008, 08:40 AM
A few initial comments:

1. $_GET["colour"], $_GET["text"] and $_GET["bg"] should be checked with isset or empty before being used.

2. stripslashes() should not be used as magic_quotes_gpc should be set to Off. If you do want to use it, use it only if get_magic_quotes_gpc() returns true.

3. There is no need to use urldecode().

4. If the width is specified but the height is not specified, there will still be an attempt to assign $_GET["height"] to $height. I suggest assigning them separately.

they would be secured, but its not really for user interaction, its for a web developer to maybe create captchas/titles in, i'll add this in the next release though, appreciate the feedback :)