2011-11-02 83 views
0

我有以下代码,它在我的PHP文件中工作得很好,并生成一个直方图图像。然而,只要我添加一个html标签,它不会再生成直方图,而是一个空的图像。在我看来,问题出现在第一行header("Content-type: image/jpeg");。但我该如何解决它?图像内容类型输出HTML标记导致问题

<html> 
    <?php 
     // Send the PNG header information. Replace for JPEG or GIF or whatever 
     header("Content-type: image/jpeg"); 

      // This array of values is just here for the example. 
      $data = array('3400','2570','245','473','1000','3456','780'); 

      // Get the total value of columns we are going to plot 
      $sum = array_sum($data); 

      // Get the height and width of the final image 
      $height = 255; 
      $width = 320; 

      $im = imagecreate($width,$height); // width , height px 

      // Generate the image variables 
      $white = imagecolorallocate($im,255,255,255); 
      $black = imagecolorallocate($im,0,0,0); 
      $red = imagecolorallocate($im,255,0,0); 


      imageline($im, 10, 5, 10, 230, $black); 
      imageline($im, 10, 230, 300, 230, $black); 

      // Now plot each column 
      $x = 15; 
      $y = 230; 
      $x_width = 20; 
      $y_ht = 0; 

      for ($i=0;$i<7;$i++){ 

       $y_ht = ($data[$i]/$sum)* $height;  

        // This part is just for 3D effect 
       imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red); 
        imagestring($im,2,$x-1,$y+10,$data[$i],$black); 

       $x += ($x_width+20); 

      } 

      imagejpeg($im); 

    ?> 
</html> 
+0

你的日志说......? –

+0

可能重复的[图形显示不正确](http://stackoverflow.com/questions/7129173/graph-not-showing-properly) – mario

回答

0

删除PHP文件中的标签,因为它不是HTML文档。 确保在 <?php 打开标签之前没有空间。

呼叫文件中和img标签一样

<img src="genrated.jpg.php" />

4

你需要两个剧本,一个是在它<img src="...">是refernces PHP文件,使图像生成HTML ...你不能直接在图像中的HTML。

+1

另外,头部语句必须在任何html输出之前发出,因此如果头部语句是正确的,它必须在任何html输出之前。 – DGM