A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearProblems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently) - page 1
User InfoPosts
Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#1
I(m trying, for reasons best known to my unconscious mind, to generate a snow-crash-like picture.

Using PHP5 and GD v2.0 (or higher), I(m using the following php/html:

<?php

$x = $y = 100;

$gd = imagecreatetruecolor($x,$y);

$w = imagecolorallocate($gd, 255, 255, 255);
$b = imagecolorallocate($gd, 0, 0, 0);


for ($r=1; $r <= $y; $r++) {

for ($c=1; $c <= $x; $c++) {


if (rand(0,1) == 0) {
$rand = $w;
}

else {
$rand = $b;
}

imagesetpixel($gd,$r,$c,$rand);

}


}


echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />

</head>

<body page="snowcrash2">

<?php

echo "<div id=\"snowcrashimg\">";



header((Content-Type: image/png();
imagepng($gd);



echo "</div>";


?>

</body>

</html>


I was trying to iterate over each (column( of each (row( of the image, setting the pixel value to either 1 or 0 to reflect either black or white.

However, this throws the (fun) error: "Warning: Cannot modify header information - headers already sent by (output started at /var/www/play/snowcrash2.php:32) in /var/www/play/snowcrash2.php on line 51"

Moving the header(...) to the first couple lines of the (in an attempt to put the header somewhere that it might get sent in time) leads to the following error (in image form): the image "http://127.0.0.1/play/snowcrash2.php" cannot be displayed, because it contains errors."

Umm...help?



The only other topic that came up is this one Generated image using PHP and GD is being cut off, which has no accepted answer and isn(t, so far as I can see, relevant to the problem I(m having.

posted date: 2009-04-07 10:17:00


Re: Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-07 10:17:01


Re: Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#3
When a browser shows an image, it is downloaded separately and placed on a page. Because of this you cannot send HTML and a picture down in one request. What you need to have is a html/php page which has an tag with the src set to another page which will only send the data for the image.eg:(index.php)<html><body><image src="pic.php" /></body></html>now in another file called (pic.php) you would generate the image and send send it back to the response with a header of "content type" and absolutely nothing else (except for other headers maybe)eg taken from http://www.webdeveloper.com/forum/showpost.php?p=879552&postcount=2<?php$number = "";for ($i = 1; $i <= $lenght; $i++){ $number .= rand(0,9)."";}$width = 11*$lenght;$height = 30;$img = ImageCreate($width, $height);$background = imagecolorallocate($img,255,255,255);$color_black = imagecolorallocate($img,0,0,0);$color_grey = imagecolorallocate($img,169,169,169);imagerectangle($img,0, 0,$width-1,$height-1,$color_grey);imagestring($img, 5, $lenght, 7, $number, $color_black);//////// VERY IMPORTANTheader((Content-Type: image/png();//////// VERY IMPORTANTimagepng($img);imagedestroy($img); ?>

posted date: 2009-04-07 10:26:00


Re: Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#4
You, sir, are a genius thank you! :) All I want to do now is turn it into a function, and be able to pass x and y values in from a form...sigh... ;)

posted date: 2009-04-07 11:00:00


Re: Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#5
No problem. Also no need to make a function, the <form> tag will be on the index.php page with the action being pic.php and then read the forms inputs by using $_REQUEST['x'] or $_GET['x'] see w3schools.com/PHP/php_get.asp for more info.

posted date: 2009-04-07 11:29:00


Re: Problems with imagesetpixel and displaying the image with php5 and gd 2.0 (or higher, apparently)#6
I <3 Stackoverflow! :)

posted date: 2009-04-07 14:16:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.