Results 1 to 3 of 3

Thread: Random image generator only generates 2 pixels

  1. #1
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default Random image generator only generates 2 pixels

    The idea here is to generate a PNG image with each pixel a random color.
    PHP Code:
    <?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)
    Last edited by techno_race; 04-11-2011 at 12:50 AM.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Here's a way of doing it with nested for loops.

    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;
            
    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.
    Last edited by Schmoopy; 04-01-2011 at 10:46 PM.

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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

  4. The Following User Says Thank You to Schmoopy For This Useful Post:

    techno_race (04-11-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •