dow58115 2014-10-06 23:03
浏览 386
已采纳

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

I am trying to use a font that I've uploaded in my website, but I keep getting this error:

"imagettftext(): Could not find/open font".

I have already tried using the putenv tool, but it still doesn't open the file. Is there an option on Bonfire that limits what kind of files that can be used? I am able to use the imagestring function, but I want to have other fonts.

I was able to load the font in an HTML file, so it looks like it has a problem with 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条回答 默认 最新

  • dongwoqin7034 2014-10-08 22:24
    关注

    GD / FreeType font loading code is restricted to the local filesystem. Your code is trying to read the font from an HTTP URL:

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

    The font-loading code in GD doesn't know how to make an HTTP request.

    Here is an example of the minimal set of your code required to get some kind of output; that is, I didn't attempt a serious rewrite in any way of your code, but also stripped out anything that was not obviously directly related to the problem:

    <?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);
    }
    

    The output of this code is visible here:

    GD code

    My recommendation would be to start with this, and see if you can get working output, and then slowly add additional code back in until you either have a working solution, or find what is breaking it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?