2011-11-02 81 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