[thelist] PHP - random image generation

Aleem Bawany aleem.bawany at utoronto.ca
Fri Jan 3 19:20:01 CST 2003


here's an improvement on the previous algorithm and some issues I
was just reading up on (random seed must be generated only once and
the color palette for jpgs is limited to 255 (gd2 has true color
support).

----------------------------------------------------
<?php
// create an image (Width x Height)
$width = 800;
$height = 100;
$img = imagecreate($width,$height);


// random seed (do not change this)
srand((double)microtime()*1000000);

// 255 is max number of colors that can be allocated
// to a jpeg image (gd2 has true color support)
for($i=0; $i<255; $i++) {
	// generate random r,g,b values
	$r = rand(0,255);
	$g = rand(0,255);
	$b = rand(0,255);

	// allocate color to line
	$linecolor = imagecolorallocate($img,$r,$g,$b);

	//draw line for each 255 pixel-wide segement
	for($j=0; $j<$width/255; $j++) {
	imageline($img,$i+($j*255),0,$i+($j*255),$height,$linecolor);
	}
}

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

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




More information about the thelist mailing list