tags:

views:

262

answers:

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"&gt;

<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.

+1  A: 

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&amp;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 IMPORTANT
header('Content-Type: image/png');
//////// VERY IMPORTANT
imagepng($img);
imagedestroy($img); 

?>
Mladen Mihajlovic
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
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 http://www.w3schools.com/PHP/php_get.asp for more info.
Mladen Mihajlovic
I <3 Stackoverflow! :)
David Thomas