[thelist] PHP - random image generation

Aleem Bawany aleem.bawany at utoronto.ca
Fri Jan 3 18:47:01 CST 2003


> I'm new to PHP and computational design. Can anyone help get
> me started on how to create the algorithm on this page?
> http://humortree.shifk.com/projects/lines02/

It's quite neat... I haven't used the GD library much, but I played with
it at some point. Anyhow, to start off with images you need to have the
GD
library installed and probably want to work with JPG/PNG formats because
of
LZW patent issues with GIFs.

Installing the library is two steps: copy the php_gd.dll in your
\windows\system32 folder and uncomment the php_gd extension
line in php.ini (then restart your web server)

what follow is the code to generate the image, it's really simple
and self explanatory. I figured I had to try this for myself (took
me a couple of minutes). Paste this code in a php file and load it up
in the browser (it's a new image everytime you reload):
-------------------------------------------------------
<?php
// create an image 200 x 100 (Width x Height)
$width = 200;
$height = 100;
$img = imagecreate($width,$height);


// draw vertical lines
for($i=0; $i<$width; $i++) {

	// generate a random number
	srand((double)microtime()*1000000);
	// allocate a color to the image palette: (image_name,R,G,B)
	// where each R, G, B is a random value between 0 and 255
	$linecolor =
imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
	imageline($img,$i,0,$i,200,$linecolor);
}

// draw the image
header("Content-type:image/jpeg");
imagejpeg($img);
?>
-------------------------------------------------------

Doesn't get any simpler than that. The example you pasted generated
a random color based on the previous color and this ones doesn't,
but it looks just as pretty. I'm sure you can work your way from here
and get the effect you want.

aleem

[ http://members.evolt.org/aleem/ ]




More information about the thelist mailing list