【问题标题】:Does anyone know why imagettftext doesn't open a font file有谁知道为什么 imagettftext 不打开字体文件
【发布时间】:2014-10-06 23:03:24
【问题描述】:

我正在尝试使用我在网站上上传的字体,但我不断收到此错误:

imagettftext(): 找不到/打开字体”。

我已经尝试过使用 putenv 工具,但它仍然无法打开文件。 Bonfire 上是否有一个选项可以限制可以使用的文件类型? 我可以使用 imagestring 函数,但我想要其他字体。

我能够将字体加载到 HTML 文件中,所以看起来 imagettftext() 有问题。

    $image = imagecreatefrompng('/home/dev3/public_html/themes/admin/images/countdown.png');
    $font = array(
        'size'=>40,
        'angle'=>0,
        'x-offset'=>10,
        'y-offset'=>70,
        'file'=>'/home/dev3/public_html/fonts/DIGITALDREAM.ttf',
        'color'=>imagecolorallocate($image, 255, 255, 255),
    );

            $image = imagecreatefrompng('/home/dev3/public_html/themes/admin/images/countdown.png');
            // Open the first source image and add the text.
            $text = $interval->format('%a:%H:%I:%S');
            if(preg_match('/^[0-9]\:/', $text)){
                $text = '0'.$text;
            }
            $text,$font['color']);
            putenv('GDFONTPATH=' . realpath('.'));
            imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'],$font['file'] ,$text);

            ob_start();
            imagegif($image);
            ob_end_clean();

    $gif = new AnimatedGif($frames,$delays,$loops);
    $gif->display();

【问题讨论】:

  • 尝试使用完整路径。或者查看echo getcwd(); 的输出。它和你的 .ttf 文件所在的位置一样吗?
  • 没有 .ttf 文件与我的 php 位于同一目录中。我已将 .ttf 文件的副本放在多个位置,包括具有正确路径的 public_html 文件夹,但它仍然给我带来了问题。我可以打开路径的 url,它会提示我下载文件。我什至尝试在不同的服务器上使用 URL,它运行良好。我认为这个问题与 imagettftext() 有关,因为 imageString 函数工作得很好,我只想使用 .ttf 文件

标签: php imagettftext bonfire


【解决方案1】:

GD / FreeType 字体加载代码仅限于本地文件系统。您的代码正在尝试从 HTTP URL 读取字体:

$font = array(...
   'file'=>'https://dev3.successengineapps.com/fonts/DIGITALDREAM.ttf'
...);

GD 中的字体加载代码不知道如何发出 HTTP 请求。

这是获得某种输出所需的最小代码集的示例;也就是说,我没有尝试以任何方式对您的代码进行认真的重写,但也删除了与问题没有明显直接关系的任何内容:

<?php
header('Content-Type: image/gif');
$image = imagecreatefrompng('https://dev3.successengineapps.com/themes/admin/images/countdown.png');
    $font = array(
        'size'=>40,
        'angle'=>0,
        'x-offset'=>10,
        'y-offset'=>70,
        'file'=>'./DIGITALDREAM.ttf',
        'color'=>imagecolorallocate($image, 255, 255, 255),
    );
$text = "Hello, world.";
if (imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'],$font['file'] ,$text)) {
        imagegif($image);
} else {
        var_dump($php_errormsg);
}

此代码的输出可见此处:

我的建议是从这个开始,看看你是否可以获得有效的输出,然后慢慢添加额外的代码,直到你有一个有效的解决方案,或者找到破坏它的地方。

【讨论】:

  • Array ( [GD 版本] => 捆绑 (2.0.34 兼容) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => [GIF Read Support] = > 1 [GIF 创建支持] => 1 [JPEG 支持] => 1 [PNG 支持] => 1 [WBMP 支持] => 1 [XPM 支持] => 1 [XBM 支持] => 1 [JIS 映射的日语字体支持] => )
  • 我知道——打开/加载 TTF 文件的代码不使用 PHP 的 streams,它使用 FreeType,它不支持 URL,只支持本地文件系统。
  • 我目前正在尝试一条路径,但它也不起作用(/home/dev3/public_html/fonts/DIGITALDREAM.ttf)
  • 您应该细化您的问题,即“有谁知道为什么 imagettftext 在使用 URL 打开文件时会出现问题?”
  • 在不同的服务器中,我有几乎相同的代码,只需添加 GDfontpath 并仅使用文件名(无路径)即可引用文件,这导致我遇到了这个问题。我在 .php 文件所在的目录中有一个副本,但这也不起作用...
猜你喜欢
  • 2021-09-04
  • 2023-03-07
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
相关资源
最近更新 更多