簡體   English   中英

imagettftext 不工作

[英]imagettftext not working

我試圖在圖像中寫入文本只是為了測試目的,因為我的 Zabbix 安裝沒有在圖表中寫入文本。 我已經從 php.net 網站復制了下面的代碼( http://php.net/manual/en/function.imagettftext.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 = '/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);
?>

有人有想法嗎?

我已經弄清楚這里出了什么問題。 訪問 truetype 文件的權限。 PHP 無法訪問該文件,因此無法寫入。

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

我的問題是錯誤的偏移量。 圖像沒有顯示任何內容,沒有文本,源代碼中沒有錯誤,只是一個空白文件。 路徑是正確的。 我以為 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);

它是一個 GD function,您需要安裝 php-gd 並支持 gettext 和 ttf。

-$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM