Take the 2-minute tour ×

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:


    		$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;






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


<body page="snowcrash2">


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


    	echo "</div>";




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 "" cannot be displayed, because it contains errors."


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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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)

<image src="pic.php" />

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


$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);

share|improve this answer
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... ;) –  David Thomas Apr 7 '09 at 19:00
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. –  Mladen Mihajlovic Apr 7 '09 at 19:29
I <3 Stackoverflow! :) –  David Thomas Apr 7 '09 at 22:16

Your Answer


