2009-04-07 77 views
0

我试图,为我的无意识的头脑最知道的原因,生成一场雪崩般的图片。imagesetpixel问题,并显示图像与php5和GD 2.0(或更高,显然)

使用PHP5和GD V2.0(或更高版本),我用下面的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> 

我试图遍历的每一个“行”的每一“列”图像,将像素值设置为1或0以反映黑色或白色。 (警告:无法修改标题信息 - 在/ var/www/play中已经发送(在/var/www/play/snowcrash2.php:32开始的输出)的标题) /snowcrash2.php on line 51“

将标头(...)移动到(尝试将标头放在某处,可能会及时发送)的第一对夫妇行会导致以下错误(以图像形式):形象“http://127.0.0.1/play/snowcrash2.php”无法显示,因为它包含错误“

嗯...帮助


唯一的其他话题就是这一个Generated image using PHP and GD is being cut off,它没有被接受的答案,而且,就我所知,与我遇到的问题有关。

回答

1

当浏览器显示图像时,它会单独下载并放置在页面上。因此,您无法在一个请求中发送HTML和图片。你需要的是一个html/php页面,它有一个标签,src被设置为另一个页面,只会发送图像的数据。

如: (的index.php)

<html> 
<body> 
<image src="pic.php" /> 
</body> 
</html> 
现在

在被称为另一个文件(pic.php),你会产生图像和发送发送回用“内容类型”的首部,响应绝对没有别的(除了其他头可能)

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

?> 
+0

采取的先生,你是一个天才* *谢谢! :)我现在想要做的就是把它变成一个函数,并且能够从窗体传递x和y值......叹息...;) – 2009-04-07 19:00:04