PHP - imagettftext无法工作且已安装了GD

9

我已经花费很长时间寻找解决这个问题的答案...

所有我发现的解决方案都跟获取字体名称有关,但是我非常确定这不是我的问题。

看起来GD已经安装好了。

array(11) {
  ["GD Version"]=>
  string(27) "bundled (**2.0.34 compatible**)"
  ["FreeType Support"]=>
  bool(false)
  ["T1Lib Support"]=>
  bool(false)
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(true)
  ["JPEG Support"]=>
  bool(true)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XPM Support"]=>
  bool(true)
  ["XBM Support"]=>
  bool(true)
  ["JIS-mapped Japanese Font Support"]=>
  bool(false)
}

以上是我的GD支持情况。 我的PHP版本是5.3,我正在运行Linux。

我尝试过来自不同网站的几个不同的代码示例,但都没有成功。 ImageString可以正常工作,但我需要让 imagettftext 正常工作。

这是我现在尝试的最后一段代码-

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

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

// Create the image
$im = imagecreatetruecolor(400, 100) or die("Can't create image!");

// 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, 'arial.ttf', $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, 'arial.ttf', $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Result: http://www.7679679.com/app/test-ansi.php


如果在imagepng之前设置header,你会得到什么错误信息? - michi
PHP会给你任何错误/通知吗?imagettftext函数返回什么?它应该返回点数组或错误时返回false。尝试$text_result = imagettftext(...); if($text_result===false){echo("ERROR");}else print_r($text_result);。另外,当你填充新创建的图像时,你在高度上留下了一个像素,只填充了100个像素中的29个,这是有意为之吗? - Buksy
1
你是怎么打印那个数组的? - Saswat
如果实际回答了您的问题,请考虑接受答案(在左侧点击勾号)。 - michi
5个回答

20

我曾经遇到过同样的问题,安装了FreeType后解决了。

 $font = "./Arial.ttf"; // <--- put ./ in front of filename

1
对我有用!哈哈!你能详细解释一下为什么这个有效吗? - Adarsh Sojitra
抱歉,我不知道为什么会这样! - michi
工作了!- 但真的很奇怪,我也想知道为什么会发生这种情况? - user6057915
1
我甚至不得不添加 realpath('./Arial.ttf')。 - ESP32
defined('ROOT_PATH') || define('ROOT_PATH', realpath(dirname(FILE) . '')); $font_path = ROOT_PATH.'/fonts/arial-italic.ttf'; - Kamlesh

8

我也遇到了一些关于IT技术的问题。

$font='arial.tff'; 

我认为你应该提供像$font这样的绝对路径,例如

$font="c:/windows/fonts/arial.ttf";

我猜您是Windows用户。 并且 删除
header('Content-Type:image/png');

获取真正的错误信息


2
添加本地路径可能不是一个完美的解决方案,也许可以使用getcwd() +文件名。 - Damiani
@Damiani,您说得完全正确,但这条路径是针对操作系统中安装的字体的,我会更新答案。感谢宝贵的建议。 - Veshraj Joshi

8

注意您尚未安装Free Type:

["FreeType Support"]=>
  bool(false)

此函数需要GD库和FreeType库的支持。

在使用此函数之前,您需要安装FreeType库。

尝试安装以下软件包:freetype, freetype-devel

如果您编译了PHP,则可以在编译时确认已启用freetype:

--with-freetype-dir=/usr/include/freetype2/ --with-freetype

如果你使用像YUM或APT-GET这样的东西,安装这些库应该非常简单,并且通过谷歌快速搜索你就能开始了。

Freetype支持的要求已在PHP手册中记录,详见http://php.net/manual/en/function.imagettftext.php和http://php.net/manual/en/function.imagettfbbox.php。 - dregad

5
我用以下解决方案解决了这个问题:
$fontfile= __DIR__.'/Fontname.ttf';

-1

安德鲁是正确的,php Imagettftext 手册 明确说明使用 imagettftextimagettfbox等功能需要 FreeType。大多数人在安装 GD 开发包时会自动安装 FreeType:

Fedora/Redhat:

yum install gd gd-devel php-gd

Debian/Ubuntu:

apt-get install php5-gd libgd2-xpm libgd2-xpm-dev

这个错误可能在你的日志中:

PHP Fatal error:  Call to undefined function imageTTFText()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接