有谁知道为什么imagettftext不会打开字体文件

时间:2014-10-06 23:03:25

标签: php imagettftext bonfire

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

  

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();

1 个答案:

答案 0 :(得分: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);
}

此代码的输出在此处可见:

GD code

我的建议是从这开始,看看你是否可以获得工作输出,然后慢慢添加其他代码,直到你有一个可行的解决方案,或找到破坏它的东西。

相关问题