PHP - imagettftext 不起作用并且安装了 GD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15000063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:21:04  来源:igfitidea点击:

PHP - imagettftext not working and GD installed

phpimagegdimagettftext

提问by Steve Peretz

It's being long hours that I'm still looking for answer to this problem.. All the solutions I find are around catching the font name but I am pretty sure this isn't my problem.

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

It looks like GD is installed

好像安装了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)
}

Above you can see my GD support. My PHP version is 5.3 and I'm running on Linux.

在上面你可以看到我的 GD 支持。我的 PHP 版本是 5.3,我在 Linux 上运行。

I have tried few different code examples from different websites and none works. ImageString does work for me but I need to get imagettftextto work..

我从不同的网站尝试了几个不同的代码示例,但没有一个有效。ImageString 对我有用,但我需要让imagettftext工作..

This is the last code I have tried now-

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

<?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

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

回答by michi

Had the same problem, with FreeType installed, solution was

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

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

回答by Andrew

Notice you don't have Free Type installed:

请注意,您没有安装 Free Type:

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

This function requires both the GD library and the ? FreeType library.

此函数需要 GD 库和 ? FreeType 库。

You will need to install Free Type library before you can use this function.

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

try installing these package:s freetype, freetype-devel

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

If you compiled PHP you can make sure you added enabled freetype during compile time:

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

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

Or if you using something such as YUM or APT-GET it should be really simple to install those libraries, and a quick search ob google with get you started.

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

回答by Veshraj Joshi

Well i also got problem around

嗯,我也有问题

$font='arial.tff'; 

I think you should provide absolute path to the $fontlike

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

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

i assume you are a windows user. and remove

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

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

to get the real error

得到真正的错误

回答by izmir_LEE

I solved this problem with this solution:

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

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

回答by im3r3k

Andrew is right, the php manual for imagettftextstates FreeType is required to use imagettftext, imagettfbox, and others. Most people the GD devel package will install FreeType automatically:

安德鲁是正确的,对于imagettftext PHP手册的FreeType需要使用状态imagettftextimagettfbox和其他人。大多数人 GD 开发包会自动安装 FreeType:

Fedora/Redhat:

Fedora/红帽:

yum install gd gd-devel php-gd

Debian/Ubuntu:

Debian/Ubuntu:

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

This error was probably in your logs:

此错误可能在您的日志中:

PHP Fatal error:  Call to undefined function imageTTFText()