imagecolorallocate - No Effect

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm having trouble with the imagecolorallocate function.  I'm creating
a security image, and the color of the text changes depending on the
background for some reason (ie, for one background the text is always
black, one is always pink, and the other is always yellow).

Any ideas?  I'm running Freebsd.

Here's my code:



$strCode = randomcode(6);
$_SESSION['strCode'] = $strCode;

$bgdir = 'images/verification/';
$bgs = array('bg1.png', 'bg2.png', 'bg3.png');
$im = ImageCreateFromPNG($bgdir.$bgs[rand(0, count($bgs)-1)]);

$font = '/usr/X11R6/lib/X11/fonts/urwfonts-ttf/n021003l.ttf';

$size = rand(12, 16);
$angle = rand(-5, 5);
//$color = ImageColorAllocate($im, rand(0, 50), rand(0, 50), rand(0,
$color = imagecolorallocate($im, 0, 0, 0);

$textsize = imagettfbbox($size, $angle, $font, $strCode);
$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);
$x = (imagesx($im)/2)-($twidth/2)+(rand(-20, 20));
$y = (imagesy($im))-($theight/2);

imagettftext($im, $size, $angle, $x, $y, $color, $font, $strCode);

header("Content-Type: image/png");





Re: imagecolorallocate - No Effect

I couldn't reproduce this.  The text was always black for me, as
expected due to the imagecolorallocate($im, 0, 0, 0).

I'm developing on Mac OS X using Safari web browser so maybe it's
something specific with FreeBSD or your web browser?

Re: imagecolorallocate - No Effect

Thanks for trying it out. Last night I resolved the problem by changing
the resolution of the background png images.  Does the
imagecolorallocate use the color pallette of the image resource you
give it?


Re: imagecolorallocate - No Effect

Looking at the documentation
( ) it seems
to imply that it does use the color pallette of the image resource.

Quoted text here. Click to load it
color that is to be used in the image represented by image."

Re: imagecolorallocate - No Effect

This is as confusing as legal documentation.  :)

But thanks... that must be what happenned.  0,0,0 must have been light
pink and light yellow in my first, low-quality png's pallettes.


Re: imagecolorallocate - No Effect

I'm glad you found the documentation confusing as well.  I thought I
was just being thick ;o)

Site Timeline