PHP - imagettftext无法正常工作并安装了GD

Ste*_*etz 7 php gd image imagettftext

我正在寻找这个问题的答案是很长时间.我找到的所有解决方案都是捕捉字体名称,但我很确定这不是我的问题.

它看起来像是安装了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)
}
Run Code Online (Sandbox Code Playgroud)

上面你可以看到我的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);
?>
Run Code Online (Sandbox Code Playgroud)

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

mic*_*chi 19

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

 $font = "./Arial.ttf"; // <--- put ./ in front of filename
Run Code Online (Sandbox Code Playgroud)


Ves*_*shi 8

嗯,我也有问题

$font='arial.tff'; 
Run Code Online (Sandbox Code Playgroud)

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

$font="c:/windows/fonts/arial.ttf";
Run Code Online (Sandbox Code Playgroud)

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

header('Content-Type:image/png');
Run Code Online (Sandbox Code Playgroud)

得到真正的错误

  • 添加本地路径可能不是一个完美的解决方案,也许使用 getcwd() + 文件名。 (2认同)

And*_*rew 7

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

["FreeType Support"]=>
  bool(false)
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

--with-freetype-dir=/usr/include/freetype2/ --with-freetype
Run Code Online (Sandbox Code Playgroud)

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


小智 6

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

$fontfile= __DIR__.'/Fontname.ttf';
Run Code Online (Sandbox Code Playgroud)