PHP生成图片海报二维码

偶然间发现一个不错的生成图片二维码类 分享一下~

class ImageSave
{

    /*
     * $file_path  二维码图片路径
     * 
     */

    public function save($file_path)
    {
        $fileName = rand(100,999).time().'.png';
        $path =./public/wechat_img/'; //存放海报 / icon图片文件夹
        $QR   = $file_path;  //二维码略缩图
        $logo = $path.'icon.png';  //小icon 原图
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二维码图片宽度
        $QR_height = imagesy($QR);//二维码图片高度
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
//重新组合图片并调整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
            $logo_qr_height, $logo_width, $logo_height);

        $QIMG = $path . rand(1000,9999) .time(). ".png";
        imagepng($QR, $QIMG);

        $dst_path = $path.'background.png';//背景图片路径
        $src_path = $QIMG;//覆盖图
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
//获取覆盖图图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);
//将覆盖图复制到目标图片上,最后个参数100是设置透明度(100是不透明),这里实现不透明效果
        imagecopymerge($dst, $src, 250, 450, 0, 0, $src_w, $src_h, 100);
        @unlink($QIMG); //删除二维码与logo的合成图片
        header("Content-type: image/png");
        imagepng($dst, $path . $fileName );//根据需要生成相应的图片
        return $path.$fileName;
    }

}

需要三张图片来合成 背景图/icon(放在二维码中间)/二维码
  • 效果图
  • 在这里插入图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值