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

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.