2

更新:经过测试,TTF 文件似乎在那里。更新:更改为字体文件的相对路径。还是不行。

当我尝试通过 PHP 使用 GD 制作图像时,出现以下错误。

[2009 年 9 月 1 日星期二 19:44:15] [错误] [客户端IP 地址] PHP 警告:imagettftext() [function.imagettftext]:在 /www/vhosts/website.com/htdocs/trial 中找不到/打开字体/TextToImage.class.php 在第 38 行

我更改了字体的路径,因为它给了我同样的错误。我通过将文件放入文件夹中将字体添加到服务器。我错过了什么?

/**
 * @name                    : makeImageF
 *
 * Function for create image from text with selected font.
 *
 * @param String $text     : String to convert into the Image.
 * @param String $font     : Font name of the text.
 * @param int    $W        : Width of the Image.
 * @param int    $H        : Hight of the Image.
 * @param int     $X        : x-coordinate of the text into the image.
 * @param int    $Y        : y-coordinate of the text into the image.
 * @param int    $fsize    : Font size of text.
 * @param array  $color       : RGB color array for text color.
 * @param array  $bgcolor  : RGB color array for background.
 *
 */
public function makeImageF($text, $font="/www/vhosts/website.com/htdocs/trial/CENTURY.TTF", $W=200, $H=20, $X=0, $Y=0, $fsize=18, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){

    $this->im = @imagecreate($W, $H)
        or die("Cannot Initialize new GD image stream");

    $background_color = imagecolorallocate($this->im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);        //RGB color background.
    $text_color = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);            //RGB color text.

    imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font, $text);
}
4

3 回答 3

3

它可能是您的 libgd 版本未知格式的字体文件。phpinfo()
输出的 gd 部分应包含 FreeType/T1Lib 库的版本。哪一个? 什么

echo '<pre>Debug: '; passthru('file '.$font); echo "</pre>\n";
// imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font, $text);

打印?

编辑:哎呀,忘记字体文件的类型。错误是Could not read font. Could not find/open font真正的意思是它所说的:要么没有这样的文件,要么无法访问。
的输出passthru('file '.$font);是“仅” CENTURY.TTF: TrueType font data?然后,您使用了相对路径。尝试将绝对路径传递给 imagettftext()

$font_realpath = realpath($font);
if ( !$font_realpath || !is_file($font_realpath) ) {
  die 'no such font file';
}
else if ( !is_readable($font_realpath) ) {
  die 'cannot read font file';
}
imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font_realpath, $text);
于 2009-09-01T20:27:32.137 回答
1

这可能是您调用函数的方式 - 将其添加到函数中以进一步帮助您

if (!file_exists($font))
    die("Font not found ($font)");
if (!is_readable($font))
    die("Font exists but not readable ($font)");

还要检查字体路径是绝对的并且不以斜杠开头,imagettftext状态的手册页

根据 PHP 使用的 GD 库的版本,当 fontfile 不以前导 / 开头时,.ttf 将附加到文件名,并且库将尝试沿着库定义的字体路径搜索该文件名。

于 2009-09-01T20:17:24.217 回答
0

Web 服务器(运行 Web 服务器的用户)是否具有对该文件夹/文件的读取权限?

于 2009-09-01T20:17:51.080 回答