Search code examples
phpimage-generation

How to generate a completely random image?


I'm trying to generate a completely random image of a given size.

Here is what I have so far:

<?php
$Width = 64;
$Height = 32;

$Image = imagecreate($Width, $Height);
for($Row = 1; $Row <= $Height; $Row++) {
    for($Column = 1; $Column <= $Width; $Column++) {
        $Red = mt_rand(0,255);
        $Green = mt_rand(0,255);
        $Blue = mt_rand(0,255);
        $Colour = imagecolorallocate ($Image, $Red , $Green, $Blue);
        imagesetpixel($Image,$Column - 1 , $Row - 1, $Colour);
    }
}

header('Content-type: image/png');
imagepng($Image);
?>

The problem is that after 4 rows it stops being random and fills with a solid colour like this
Sample of problem


Solution

  • if you change imagecreate to imagecreatetruecolor it should work (everything else is the same, including the parameters)