I'm looking to make an image that's just noise, maybe something like this:
(source: loriswebs.com)
Ideally I'd like to be able to change the colour as well. Any ideas on how to generate this?
It's fairly straightforward to generate random noise. You can accomplish this pretty easily with some of PHP's image libraries, including the GD functions. I'm sure it would be similar in ImageMagick.
If you wanted to generate completely random noise, you could use random values for every color and every pixel. That might look something like this with GD:
//random colored noise
$x = 150;
$y = 150;
$im = imagecreatetruecolor($x,$y);
for($i = 0; $i < $x; $i++) {
for($j = 0; $j < $y; $j++) {
$color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($im, $i, $j, $color);
}
}
header('Content-Type: image/png');
imagepng($im);
Generates this:
However, the example image that you posted clearly doesn't look like completely random color noise. It seems more like an arbitrary choice between one of two colors, either a somewhat-grey pixel or a somewhat-colored pixel. You could accomplish that more like this:
//two-color random noise
$x = 150;
$y = 150;
$im = imagecreatetruecolor($x,$y);
$color1 = imagecolorallocate($im, 200, 240, 242);
$color2 = imagecolorallocate($im,220,220,220);
imagefill($im,0,0,$color1);
for($i = 0; $i < $x; $i++) {
for($j = 0; $j < $y; $j++) {
if (mt_rand(0,1) == 1) imagesetpixel($im, $i, $j, $color2);
}
}
header('Content-Type: image/png');
imagepng($im);
Generates this:
Your example seems a bit more complex still, with the pixels seeming to appear in small groups to produce a blockier appearance. You could emulate that by adjusting the loop logic if you wanted, or coloring small squares instead of individual pixels.
An interesting thing about this type of generation is that you can actually see the breakdown of the rand()
function on Windows platforms if you use it instead of mt_rand()
. Discernible patterns can develop in the noise due to limitations in that function/platform combination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With