Jump to content

[SOLVED] Loop problem in GD


AV1611

Recommended Posts

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);
?> 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.