2011-10-25 83 views
0

我使用這個代碼,如果我評論的行號「A」和「B」來創建圖像
是否可以在PHP中使用動態生成的圖像?

<?php 

// Set the content-type 
header('Content-Type: image/png'); 

// Create the image 
$im = imagecreatetruecolor(400, 30); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 
$text = 'Testing...'; 
// Replace path by your own font path 
$font = 'arial.ttf'; 

// Add some shadow to the text 
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

    // Add the text 
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

    // Using imagepng() results in clearer text compared with imagejpeg() 
    (A)print ('<div class="test">'); 
    imagepng($im); 
    print ('</div>'); 
    (B)imagedestroy($im); 
    ?> 

的代碼工作的罰款和它產生在寫有測試瀏覽器的圖像。但我想要圖像在div中。所以我取消註釋行(A)和(B),但它沒有給出正確的輸出。生成的HTML也怪生成的html是

<img src="http://localhost/php/test92.php" alt="The image 「http://localhost/php/test92.php」 cannot be displayed, because it contains errors."> 
+2

你必須學習HTML是什麼,它是如何工作。沒有冒犯,只是一個友好的建議。 –

+0

@ Col.Shrapnel感謝您的建議。 –

回答

1

基本上,創建HTML動態圖像,則需要2個PHP文件:

  1. 一個圖像本身
  2. 另一個用於PHP來顯示它。

讓我們來看看你做到這一點:

  1. 創建image.php接受參數,如:圖像ID或文件名。出於安全原因,你必須過濾它得到的任何參數。

    爲什麼你必須這樣做?因爲要生成圖像,您不能將其與其他HTML輸出混合使用。更不用說單一的spacereturn,因爲這會使圖像斷裂。

  2. 你在另一個PHP上做HTML的事情,比如test92.php。到HTML的邏輯在這裏,如:

    1. 獲取圖像數據
    2. 循環中的數據
    3. 顯示圖像=><img src="image.php?imageID=12" alt="" />
+0

非常感謝您的幫助。我正在找那個。 –

1

如果你想在你的形象一個div你必須做的是,在HTML中,你不能這樣做,在圖像生成代碼

<div> 
<img src="http://localhost/php/test92.php"> 
</div> 

如果您收到有關圖片的錯誤,請嘗試瀏覽圖片url http://localhost/php/test92.php,看看它是什麼樣子。

它顯示出像你期待的圖像嗎?

相關問題