Sorry to double post, but I've now realised why doing the nested while loops like so will not work:
PHP Code:
header ('Content-type: image/png');
$width = (isset($_GET['width'])) ? $_GET['width'] : 50;
$height = (isset($_GET['height'])) ? $_GET['height'] : 50;
$image = @imagecreatetruecolor($width, $height)
or die('Cannot Initialize new GD image stream');
$x = 0;
$y = 0;
while ($y <= $height) {
while ($x <= $width) {
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$color = imagecolorallocate($image,$red,$green,$blue);
imagesetpixel($image,$x,$y,$color);
$x++;
}
$y++;
}
imagepng($image);
imagedestroy($image);
The code above will fill out the entire first row, and then no more. Basically what happens is, the $x variable is set to 50 (or whatever the width is) after the first iteration of the first while loop (the y loop), and so when the next iteration is processed, $x is already at 50 and so the commands in the while loop don't get executed.
To keep using the while loops, just reset the $x variable on each iteration of the y loop:
PHP Code:
<?php
header ('Content-type: image/png');
$width = (isset($_GET['width'])) ? $_GET['width'] : 50;
$height = (isset($_GET['height'])) ? $_GET['height'] : 50;
$image = @imagecreatetruecolor($width, $height)
or die('Cannot Initialize new GD image stream');
$y = 0;
while ($y <= $height) {
$x = 0;
while ($x <= $width) {
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$color = imagecolorallocate($image,$red,$green,$blue);
imagesetpixel($image,$x,$y,$color);
$x++;
}
$y++;
}
imagepng($image);
imagedestroy($image);
?>
This is kind of drawn out, it's a fairly simple concept, but I just got my head around it, hope this is useful to you.
Cool idea btw
Bookmarks