Log in

View Full Version : Resolved Random image generator only generates 2 pixels



techno_race
04-01-2011, 10:23 PM
The idea here is to generate a PNG image with each pixel a random color.

<?php
header ('Content-type: image/png');
$width = $_GET['width'];
$height = $_GET['height'];
$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 = $x . 1;
}
$y = $y . 1;
}
imagepng($image);
imagedestroy($image);
?>
Unfortunately, only the upper-leftmost pixel and the one to the right of it are generated; the rest of the image remains black.
Why does this happen? (Live at http://thetortoise.tk/Untitled-3.php?width=50&height=50)

Schmoopy
04-01-2011, 10:38 PM
Here's a way of doing it with nested for loops.



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;

for($y=0; $y <= $height; $y++) {
for($x=0; $x <= $width; $x++) {
$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 uses "$x++" instead of "$x = $x . 1", as that would concatenate the string, and not increment it.

That's why it wasn't looping correctly. I'm looking into why the nested while loops weren't working though.

It seems that it would do one iteration on the first row, and then not fill in the rest e.g.

[X][X][X][X][X]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]

So it would do the filling across the x axis, but not the y axis - the for loops solve that though.

Schmoopy
04-01-2011, 10:56 PM
Sorry to double post, but I've now realised why doing the nested while loops like so will not work:




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