PHP使用GD库生成签名图片png图片

需求:根据输入的名字,自动计算名字字符的长度,来生成相应的签名png图片

难点: 自动计算输入文本的长度

    public static function generateSignImage($sign_name){
        if(empty($sign_name)){
            return ['status' => false];
        }
        $height = 95;

        $ttfPath = __DIR__.'/yizhiqingshu.ttf';

        $w = static ::charwidth(56,0,$ttfPath,$sign_name);
        $width = $w + 80 + 80;

        $image = imagecreatetruecolor($width, $height);

        //2.上色
        $color=imagecolorallocate($image,255,255,255);

        //3.设置透明
        imagecolortransparent($image,$color);

        imagefill($image,0,0,$color);

        $text_color = imagecolorallocate($image, 26, 26, 26);

        imagettftext($image, 56, 0, 80 , 76, $text_color, $ttfPath, $sign_name);

        ob_start();   //启用输出缓冲
        imagepng($image);    //输出图像
        $imagebin=ob_get_contents();    //将缓冲的数据存入变量
        ob_end_clean();    //结束并清空输出缓存

        imagedestroy($image);



        $res = UploadFromServerHelper::uploadImageFromStream($imagebin);

        //var_dump($res);
        return $res;

    }


    static function charwidth($fontsize,$fontangle,$ttfpath,$char){
        $box = imagettfbbox($fontsize,$fontangle,$ttfpath,$char);
        $width = abs(max($box[2], $box[4]) - min($box[0], $box[6]));
        return $width;
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值