【问题标题】:PHP - imagettftext not working and GD installedPHP - imagettftext 不工作并且安装了 GD
【发布时间】:2013-02-06 15:32:09
【问题描述】:

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

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

【问题讨论】:

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

标签: php image gd imagettftext


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我也有问题

    $font='arial.tff'; 
    

    我认为您应该像

    一样提供 $font 的绝对路径
    $font="c:/windows/fonts/arial.ttf";
    

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

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

    得到真正的错误

    【讨论】:

    • 添加本地路径可能不是一个完美的解决方案,可能使用 getcwd() + 文件名。
    • @Damiani 你是绝对正确的,但是这个路径是针对安装在操作系统中的字体,我会更新答案。感谢您的宝贵建议
    【解决方案3】:

    Andrew 是对的,php manual for imagettftext 声明 FreeType 需要使用 imagettftext、imagettfbox 等。大多数人 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()
    

    【讨论】:

      【解决方案4】:

      遇到同样的问题,安装了 FreeType,解决方法是

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

      【讨论】:

      • 为我工作!哈哈!你能详细解释一下为什么会这样吗?
      • 对不起,我不知道为什么会这样!
      • 成功了! - 但真的很奇怪,也想知道为什么会这样?
      • 我什至不得不添加 realpath('./Arial.ttf')
      • 定义('ROOT_PATH') || define('ROOT_PATH', realpath(dirname(FILE) . '')); $font_path = ROOT_PATH.'/fonts/arial-italic.ttf';
      【解决方案5】:

      请注意您没有安装 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 快速搜索来帮助您入门。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多