Jump to content

Looping PNG coordinates ?


Murchu

Recommended Posts

Hi friends

 

basic question for the community

 

I want to generate a checkered flag but im unsure how to achieve this with for loops ?

 

each box is of side 40 and width over all = $n any help would be great thanks

 

 

$height = $n * 40;

$width = $height;

$im = imagecreatetruecolor($width, $height);

 

$white = imagecolorallocate($im, 255, 255, 255);

$red = imagecolorallocate($im, 255, 0, 0);

 

for($i = 0; $i <= $width; $i += 40)

{

for($j = 0; $j <= $n; $j += $n)

{

if(( $i + $j ) % 2 == 0)

{

imagefilledrectangle ( $im , 0, $j, 0 , $width , $red );

}

else

{

imagefilledrectangle ( $im , $j, $j , 0 , $width , $white );

}

}

}

 

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

Link to comment
https://forums.phpfreaks.com/topic/274609-looping-png-coordinates/
Share on other sites

try

 

$n = 6;
$height = $n * 40;
$width = $height;
$im = imagecreatetruecolor($width, $height);

$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);

for($i = 0; $i < $n; $i++)
{
  for($j = 0; $j < $n; $j++)
  {
  $c = ( $i + $j ) % 2 == 0 ? $red : $white;
  imagefilledrectangle ( $im , $i*40, $j*40,  ($i+1)*40 , ($j+1)*40 , $c );
  }
}

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

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.