homepage Welcome to WebmasterWorld Guest from 54.226.147.84
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
GD color limit is too low
Skier88

5+ Year Member



 
Msg#: 4291612 posted 10:11 pm on Apr 3, 2011 (gmt 0)

I just started learning about the GD library and made a small script to test a few features. I quickly found out that I was limited to 256 colors, but that creating the image with imagecreatetruecolor() instead of imagecreate() all but eliminates the issue. However, after a few modifications I saw the 256 color limit reappear, even though I am still using imagecreatetruecolor(). What is causing this and how do I fix it?

Here's my testing / playing around script. You'll notice that if you set the number of line segments (for loop's upper bound) to greater than 256 the line appears increasingly disjointed. This is because the rest of the line in being drawn in the background color, black.

<?php
$s=500;
$im=imagecreatetruecolor($s,$s);
$tmp=rand(1,$s-1);
$x=rand(0,1)?(rand(0,1)?0:$s-1):$tmp;
$y=$x&&$x<$s-1?(rand(0,1)?0:$s-1):$tmp;
$vx=0;
$vy=0;
for($i=0;$i<1000;$i++) {
$_x=$x;
$_y=$y;
$x=$x+(rand(0,1)?1:-1)+$vx;
$y=$y+(rand(0,1)?1:-1)+$vy;
if($x<0) {
$x=0;
$vx=-$vx;
}
if($x>=$s) {
$x=$s-1;
$vx=-$vx;
}
if($y<0) {
$y=0;
$vy=-$vy;
}
if($y>=$s) {
$y=$s-1;
$vy=-$vy;
}
$vx=$vx*.8+($x-$_x)*.2;
$vy=$vy*.8+($y-$_y)*.2;
$c=imagecolorallocate($im,0,(255-$i)%255,$i%255);
imageline($im,$_x,$_y,$x,$y,$c);
}
imagefilter($im,IMG_FILTER_GAUSSIAN_BLUR);
header('Content-type:image/png;');
imagepng($im);
imagedestroy($im);
?>

 

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4291612 posted 4:28 pm on Apr 28, 2011 (gmt 0)

The RGB components cannot go beyond that value:

These parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF.


[php.net...]

Little_G

5+ Year Member



 
Msg#: 4291612 posted 9:59 pm on Apr 28, 2011 (gmt 0)

Hi,

Your script is, in some instances, calling
imagecolorallocate with the third argument (green) as a negative number, this causes imagecolorallocate to return false, which imageline is interpreting as black.
Also you're always setting the red argument to 0 which will limit you range of colours.

Andrew

Skier88

5+ Year Member



 
Msg#: 4291612 posted 7:36 pm on Apr 29, 2011 (gmt 0)

Well that's embarrassing. Thanks for your replies though, and hopefully next time I'll wait a little longer before asking for help.

Little_G

5+ Year Member



 
Msg#: 4291612 posted 9:28 pm on Apr 29, 2011 (gmt 0)

Don't worry can't-see-the-wood-for-the-trees syndrome is common amongst programmers, I know because I sometimes struggle with the condition myself.

Andrew

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved