AV1611 Posted October 5, 2007 Share Posted October 5, 2007 Can anyone tell me why this image quits drawing after 255 lines? <?php header("Content-type: image/png"); $im = @imagecreate(255, 512) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 255); $q=rand(1,3); $a=0; $b=0; $c=0; $i=0; while($i <= 511){ if($q==1){$color=imagecolorallocate($im,$b,$c,$a);} if($q==2){$color=imagecolorallocate($im,$a,$b,$c);} if($q==3){$color=imagecolorallocate($im,$c,$a,$b);} imageline($im, 0, $i, 254, $i, $color); if($i < 255) {$a++;} $i++; } imagepng($im); imagedestroy($im); ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 because the image is only 255 pixels wide? Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 5, 2007 Author Share Posted October 5, 2007 Unless I'm just blind, $i is on the Y axis, and that is set to 512... Or am I really confused :/ Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 of-the-top guess. here is the result i get: http://www.blueskyis.com/test.php is that right, or what are you seeing? Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 5, 2007 Author Share Posted October 5, 2007 That yields a 255x255 box. It should be 255 wide x 512 high Why is it not drawing after the 255th line? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted October 5, 2007 Share Posted October 5, 2007 Just had a play, see if this works: <?php header("Content-type: image/png"); $im=@imagecreate(255, 512) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 255); $q=rand(1,3); $a=0; $b=0; $c=0; for ($i=0;$i<512;$i++) { if ($q==1) {$color=imagecolorallocate($im,$b,$c,$a);} if ($q==2) {$color=imagecolorallocate($im,$a,$b,$c);} if ($q==3) {$color=imagecolorallocate($im,$c,$a,$b);} imageline($im, 0, $i, 254, $i, $color); if ($i < 255) {$a++;} } imagepng($im); imagedestroy($im); ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 5, 2007 Share Posted October 5, 2007 Well, with a quick test of moving the call to imagecolorallocate() outside the loop, it appears that there might be a limit on the number of times you can allocate a colour. I've looked at using imagecolordeallocate(), but i can't get it to work. Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 5, 2007 Author Share Posted October 5, 2007 Same problem. Box is 255x255 not 255x512 Funny thing is, this is 255x512... is this a bug? <?php header("Content-type: image/png"); $im = @imagecreate(255,512) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 255); $q=rand(1,3); $a=50; $b=50; $c=50; $i=0; $color=imagecolorallocate($im,$b,$c,$a); while ($i<=512){ imageline($im, 0, $i, 254, $i, $color); $i++; } imagepng($im); imagedestroy($im); ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 Well, with a quick test of moving the call to imagecolorallocate() outside the loop, it appears that there might be a limit on the number of times you can allocate a colour. I've looked at using imagecolordeallocate(), but i can't get it to work. that's what i'm seeing here, too. very odd. could be a bug. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 5, 2007 Share Posted October 5, 2007 Indeed. A google search confirms thats the problem, and you can get round it by using imagecreatetruecolor() rather than imagecreate(). Quote Link to comment Share on other sites More sharing options...
Yesideez Posted October 5, 2007 Share Posted October 5, 2007 I copied the image and pasted into MSPaint and I can see that a 255x512 image is being made just that the bottom half is remaining empty. Just for debugging purposes, change the background color to something contrasting like 255,0,255. Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 5, 2007 Author Share Posted October 5, 2007 Well, I got it to work. I change from imagecreate() to imagecreatetruecolor() and it works. Even though I am only using 256 colors, I think it gets mad if you call a color more than 256 times... it treats each as a color on the pallet maybe? Dunno, but either way, that's one to remember... Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2007 Share Posted October 5, 2007 If I run your original posted code I get a completely filled image 255x512 with a gradient from black to blue (or red or green) for the whole of the image; black at top, blue (or red or green) at bottom. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 odd, what version of PHP and GD are you running? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2007 Share Posted October 5, 2007 PHP : 5.1.2 GD : 2.0.28 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 hm, i'm at PHP 5.2.0, GD 2.0.28 and it doesn't work for me. i am curious... Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2007 Share Posted October 5, 2007 I get same (correct) result with PHP 4.3.9, GD 2.0.28 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2007 Share Posted October 5, 2007 Resulting output attached [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i get correct ouput in 4, but not 5, same GD version. oh well, i can live with it, but it's good to know in case i see anything similar happen elsewhere. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.