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
https://forums.phpfreaks.com/topic/72023-solved-loop-problem-in-gd/
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);
?>

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.

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

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.

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

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.