2011-08-11 40 views
1

我試圖在圖像中寫文本只是爲了測試目的,因爲我的Zabbix安裝不是在圖形中寫文本。我已將php.net網站的代碼複製下來(http://php.net/manual/en/function.imagettftext.phpimagettftext不能正常工作

<?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 = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.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

描述問題。什麼不起作用? – Phil

+0

我弄清楚這裏有什麼問題。訪問truetype文件的權限。 PHP無法訪問該文件,因此無法寫入。 我沒有看到問題,因爲我沒有使用E_ALL運行。現在一切順利運行 – gawry

回答

3

我弄清楚這裏有什麼問題。訪問truetype文件的權限。 PHP無法訪問該文件,因此無法寫入。

我沒有看到問題,因爲我沒有運行E_ALL。現在一切順利運行

0

這是一個GD函數,你需要安裝php-gd和gettext & ttf支持。

- $

+1

GD現在已經與PHP捆綁在一起。另一個需求是我知道的[FreeType](http://www.freetype.org/)庫 – Phil

+0

。還可以。我可以畫畫,而且我可以寫出沒有truetype。問題在於訪問字體文件。 – gawry

0

我的問題是一個錯誤的抵消。該圖像沒有顯示任何內容,沒有文字,源代碼中沒有錯誤,只是一個空白文件。路徑是正確的。我以爲ttf字體出現了錯誤,但事實證明這只是錯誤的定位。

這裏就是幫助我去看了一下文字:

imagettftext($im, 20, 0, 20, 20, $fg, $font, $text); 

這顯示在右上方一些文字。

全部工作代碼:

putenv('GDFONTPATH=' . dirname(__FILE__)); 
    $font = 'arial'; // located next to the script 
    imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);