2016-02-13 46 views
1

我輸入此代碼以顯示文本轉換爲圖像。imagettftext()函數不工作?

<?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() 
imagepng($im); 
imagedestroy($im); 
?> 

並在此之後的輸出是::

Output in mozilla

我在Windows中使用WAMP還安裝了GD ...我gd_info():

array (size=12) 
'GD Version' => string 'bundled (2.1.0 compatible)' (length=26) 
'FreeType Support' => boolean true 
'FreeType Linkage' => string 'with freetype' (length=13) 
'T1Lib Support' => boolean false 
'GIF Read Support' => boolean true 
'GIF Create Support' => boolean true 
'JPEG Support' => boolean true 
'PNG Support' => boolean true 
'WBMP Support' => boolean true 
'XPM Support' => boolean true 
'XBM Support' => boolean true 
'JIS-mapped Japanese Font Support' => boolean false 

所以最後我必須做...請幫助我......

+0

你有沒有試過在編輯器中查看文件? – Zulan

回答

0

試試我的代碼......創建一個PHP文件 - >把這段代碼,只是運行你 得到了肯定的結果...快樂作弄....

不要忘了加「arial.ttf」文件,你在哪裏運行你的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() 
     imagepng($im); 
     imagedestroy($im); 
?> 
+0

感謝它的作品....你會解釋關於錯誤plz ........ –

+0

我認爲...那裏你忘了pur arial.ttf文件 –

+0

它看起來類似於原始代碼。現在怎麼運作?謹慎解釋? OP表示arial.ttf已經存在於腳本所在的目錄中。 –

0

如果它在你後面工作對兩個調用imagettftext()的註釋,確保文件arial.ttf存在於腳本的相同目錄中。另外註釋掉對header()的調用,以便能夠查看返回的任何錯誤消息。

+0

yah ....字體文件在同一目錄中 –

+0

嘗試將'arial.ttf'更改爲'arial' –

+0

從文檔http://php.net/manual/en/function.imagettftext.php「根據哪個版本的PHP庫正在使用的GD庫,當fontfile不以前導符/ /開頭時,則.ttf將被附加到文件名,並且庫將嘗試沿着庫定義的字體路徑搜索該文件名。「 –