PHP - imagettftext 不工作并且安装了 GD

问题描述 投票:0回答:6

已经很长时间了,我仍在寻找这个问题的答案.. 我找到的所有解决方案都是围绕捕获字体名称,但我很确定这不是我的问题。

看起来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);
?>

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

php image gd imagettftext
6个回答
21
投票

有同样的问题,安装了 FreeType,解决方案是

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

8
投票

注意您没有安装 Free Type:

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

此功能需要 GD 库和 » FreeType 库。

您需要先安装Free Type库才能使用此功能。

尝试安装这些软件包:s freetype, freetype-devel

如果您编译了 PHP,您可以确保在编译时添加了启用的 freetype:

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

或者,如果您使用 YUM 或 APT-GET 之类的东西,那么安装这些库应该非常简单,并且可以快速搜索 google 来帮助您入门。


8
投票

我也遇到了问题

$font='arial.tff'; 

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

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

我假设您是 Windows 用户。 并删除

header('Content-Type:image/png');

得到真正的错误


6
投票

我用这个解决方案解决了这个问题:

$fontfile= __DIR__.'/Fontname.ttf';

0
投票
$fontfile= __DIR__.'/Fontname.ttf';

此代码对我有用


-1
投票

Andrew 是对的,imagettftext 的 php 手册指出,使用

imagettftext
imagettfbox
等需要 FreeType。大多数人的 GD 开发包会自动安装 FreeType:

软呢帽/红帽:

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()
© www.soinside.com 2019 - 2024. All rights reserved.