Jump to content

imagecolourallocate


plutomed

Recommended Posts

imagecolourallocate is just returning black all the time.

 

If I call this image via this link:

page.php?page=captcha&backColour=0xFF|0xFF|0xFF&textColour=0x00|0x00|0x00

I get just black for all values. If I set the values manually in the code it works perfectly fine. I'm a bit confused.  :confused:

 

/****************************\
|           Captcha          |
\****************************/
if($_GET['page'] == "captcha")
{
$chars = "ABCDEFGHJKLMNPQRTUVWXYZabcdefghjklmnpqrtuvwxyz2346789";
$chars = preg_split('//', $chars);
$string = "";
$backColour = explode("|",$_GET['backColour']);
$textColour = explode("|",$_GET['textColour']);
srand(time());
for($i=0; $i<6; $i++)
{
	$string .= $chars[rand(0, 52)];
}
$im = imagecreate(75,25);

//Background colour
//imagecolorallocate($im, 0xFF,0xFF,0xFF);
imagecolorallocate($im, $backColour[0],$backColour[1],$backColour[2]);

//Border
imageline($im, 0,0,74,0,imagecolorallocate($im, $textColour[0],$textColour[1],$textColour[2]));
imageline($im, 0,0,0,24,imagecolorallocate($im, $textColour[0],$textColour[1],$textColour[2]));
imageline($im, 74,0,74,24,imagecolorallocate($im, $textColour[0],$textColour[1],$textColour[2]));
imageline($im, 0,24,74,24,imagecolorallocate($im, $textColour[0],$textColour[1],$textColour[2]));

imagestring($im, 5, 5, 5, $string, imagecolorallocate($im, $textColour[0],$textColour[1],$textColour[2]));
imagepng($im);
$_SESSION['captcha'] = $string;

imagedestroy($im);
}

Link to comment
https://forums.phpfreaks.com/topic/204638-imagecolourallocate/
Share on other sites

Ok, instead of keeping the values as hex I have converted them to decimal. It now works. Still dunno why it didn't work with hex though. :/ Does anyone have any clue?

 

The way that PHP converts strings to integers is by reading all characters until the first non-digit character is found. This means that (int)"0xFF" becomes 0 and not 255.

 

See: http://dk.php.net/manual/en/language.types.string.php#language.types.string.conversion

Link to comment
https://forums.phpfreaks.com/topic/204638-imagecolourallocate/#findComment-1071422
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.