熊猫叶 OTO
分享
  • 收藏
  • 举报
    X
    imagettftext windows环境下无法生成字体
    733
    7
    //处理文字
            if (!empty($config['text'])) {
                // 验证码使用随机字体
                // $ttfPath = './public/static/plugins/posters/zhttfs/1.ttf';  //这样子写在linux环境下是没有问题的,php官方也是这样子给的样例,但用在win下是无法打上文字的
                $ttfPath = realpath(".").'/public/static/plugins/posters/zhttfs/1.ttf';//这样子就可以在win上打上文字,路径识别问题
    
                foreach ($config['text'] as $key => $val) {
                    $val = array_merge($textDefault, $val);
                    $val['left'] = ($val['left'] * $backgroundWidth) . 'px';
                    $val['top'] = ($val['top'] * $backgroundHeight) . 'px';
                    $val['width'] = ($val['width'] * $backgroundWidth) . 'px';
                    $val['height'] = ($val['height'] * $backgroundHeight) . 'px';
                    //判断字体颜色是十六进制还是RGB
                    if (strstr($val['fontColor'], ',')) {
                        list($R, $G, $B) = explode(',', $val['fontColor']);
                    } else {
                        $val['fontColor'] = str_replace('#', '', $val['fontColor']);
                        if (strlen($val['fontColor']) > 3) {
                            $R = hexdec(substr($val['fontColor'], 0, 2));
                            $G = hexdec(substr($val['fontColor'], 2, 2));
                            $B = hexdec(substr($val['fontColor'], 4, 2));
                        } else {
                            $R = substr($color, 0, 1) . substr($color, 0, 1);
                            $G = substr($color, 1, 1) . substr($color, 1, 1);
                            $B = substr($color, 2, 1) . substr($color, 2, 1);
                        }
                    }
                    $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
                    $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
                    $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
                    imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $ttfPath, $val['text']);
                }
            }

    因有的用户用的是win服务器,安装海报的时候设置的文字内容无法生成上,但在linux是正常的,所以测试后发现是路径问题。

    因为官方及很多的案例都是$font ='./times.ttf'; 这样子写的。导致win无法识别。

    需要realpath()用来将参数path所指的相对路径转换成绝对路径。


    特记录下来...

    8
    赏礼
    赏钱
    收藏
    点击回复
        全部留言
    • 7
    • 金森 实战运营者 消费:1479元 2021-06-08 11:51 11:516楼
    0 赏钱 赏礼回复
    • 西华 普通粉丝 消费:2507.08元 2021-06-07 15:55 15:556楼
      有始有终,对自己的产品要负责
    0 赏钱 赏礼回复
    • 千里寻花 普通粉丝 消费:60.02元 2021-06-07 15:42 15:42
      华为
      6楼
      问题
    0 赏钱 赏礼回复
    • Array 消费:15元 2021-06-07 14:35 14:356楼
      感谢分享
    0 赏钱 赏礼回复
    • wr1688 风格开发者 消费:2593.25元 2021-06-07 14:10 14:10
      华为
      6楼
      优秀
    0 赏钱 赏礼回复
    • 无痕 青铜粉丝 消费:517.7元 2021-06-07 13:16 13:166楼
    0 赏钱 赏礼回复
    • 袁冬 普通粉丝 消费:160.85元 2021-06-07 12:23 12:23
      华为
      6楼
      打个印
    0 赏钱 赏礼回复
    更多回复
    恢复多功能编辑器
  • 3 1
  • 熊猫叶 OTO
        圈内贴子441
    • 圈子成员84
    本圈子内的新贴

    推荐内容
    扫一扫访问手机版
    请选择要切换的马甲:

     
    网页即时交流
    QQ咨询
    咨询热线
    020-28998648