imagettftext不会工作;文字没有显示出来

时间:2014-01-19 12:10:40

标签: php gd

我正在尝试使用imagettftext()通过PHP向图像添加文本。到目前为止,我已经完成了大约6个不同的教程,试图让它工作,我没有成功。我目前正在从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);
?>

然而,没有任何表现。我安装了GD库,并启用了freetype。我有arial.ttf在与php文件相同的目录中,我无法弄清楚它为什么不起作用。我得到的只是一张空白图片。

编辑:这是错误消息“[19-Jan-2014 16:31:07] PHP警告:imagettftext():无法在/var/www/php/bb/test.php中找到/打开字体第21行“

1 个答案:

答案 0 :(得分:0)

我正打算告诉你输入ttf的实际路径而不是相对路径。

putenv('GDFONTPATH=' . realpath('.'));

$font = '/home/user/arial.ttf';
相关问题