Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why only 256 pixels? 1

Status
Not open for further replies.

PockyBum522

Programmer
Jun 10, 2003
239
US
Hello, I'm new to php and working on someone else's code, however I have plenty of experience in programming in other languages. My question is, why does this script that should fill a square png with each pixel being a random color only fill the first 256 pixels?

<?php
$height = 50;
$width = 50;
$cypher_img = imagecreate($width, $height);

$total = $width * $height;
$nowx = 0;
$nowy = 0;

while($nowx * $nowy < $total) {
$red = rand(0, 255);
$green = rand(0, 255);
$blue = rand(0, 255);

$randcolor = ImageColorAllocate($cypher_img, $red, $green, $blue);
imagesetpixel($cypher_img, $nowx, $nowy, $randcolor);
$nowx++;
//imagecolordeallocate ($randcolor, $cypher_img);
if($nowx > $width){$nowy++; $nowx = 0;}
}

header ("Content-type: image/png");
ImagePng ($cypher_img);
ImageDestroy($cypher_img);
?>

Also, why does imagecolordeallocate put errors in the image if you un-remark it at the end there?

Thank you!
 
Your problem is not that only 256 pixels are being randomly set. Your problem is that you can only allocate 256 colors. The part of the image which is not randomly pixellated is set to a single color, randomly-chosen each run of the script. Basically, your image runs out of colors that can be allocated, and the 256th color is used to fill in the rest of the image.

And you're getting errors in the imagecolordeallocate() invocation, as the error message states, because the first parameter passed the function is not an image resource. You're passing the parameters in the wrong order.

Your script (minus the imagecolordeallocate() invocation) runs fine if you use imagecreatetruecolor() in place of your invocation of imagecreate(). Imagecreatetruecolor() is the recommended function of the PHP online manual. An image resource created with imagecreatetruecolor() will also allow more than 256 colors.

Try this version of your script on for size:
Code:
<?php
$height = 480;
$width = 640;
$cypher_img = imagecreatetruecolor($width, $height);

for ($nowy = 0; $nowy <= $height; $nowy++)
{
	for ($nowx = 0; $nowx <= $width; $nowx++)
	{
		$red = rand(0, 255);
		$green = rand(0, 255);
		$blue = rand(0, 255);
		$randcolor = imagecolorallocate($cypher_img, $red, $green, $blue);
		imagesetpixel($cypher_img, $nowx, $nowy, $randcolor);
	}
}

header ("Content-type: image/png"); 
imagepng ($cypher_img); 
imagedestroy($cypher_img);
?>

The following version will allocate only a specified number of colors and randomly pixelate the entire image with those colors:

Code:
<?php
$height = 480;
$width = 640;
$number_of_colors = 256;
$cypher_img = imagecreatetruecolor($width, $height);

$color_array = array();
for ($color_counter = 0; $color_counter < $number_of_colors; $color_counter++)
{
	$red = rand(0, 255);
	$green = rand(0, 255);
	$blue = rand(0, 255);
	$color_array[] = imagecolorallocate($cypher_img, $red, $green, $blue);
}

for ($nowy = 0; $nowy <= $height; $nowy++)
{
	for ($nowx = 0; $nowx <= $width; $nowx++)
	{
		$randcolor = rand(0,$number_of_colors - 1);
		imagesetpixel($cypher_img, $nowx, $nowy, $color_array[$randcolor]);
	}
}

header ("Content-type: image/png"); 
imagepng ($cypher_img); 
imagedestroy($cypher_img);
?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top