TP5 生成二维码并合成图片

1.生成二维码

   vendor('phpqrcode.phpqrcode');
    $url = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIzODQ1NzY2MQ==&scene=124#wechat_redirect&sid='.$uid;
    $errorCorrectionLevel = 'H';//容错级别
    $matrixPointSize = 5;//图片大小慢慢自己调整,只要是int就行
    $path = 'user_cache/';
    $QR =$QRB = $path.rand(10000,99999).time().".png";
    \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

2.二维码与logo合成

    $head = db('user')->where(['id'=>$uid])->find();
    $logo =isset($head['avatar'])?$head['avatar']:'';
    if ($logo !== FALSE) {
        $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(100000,999999).time().".png";
    imagepng($QR, $QIMG);

3.将第二步生成的图片与背景图合并


    $dst_path = 'lALPBbCc1VATaEvNA4DNAnw_636_896.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, 175, 400, 0, 0, $src_w, $src_h, 100);
     @unlink($QIMG); //删除二维码与logo的合成图片
     @unlink($QRB);  //删除服务器上二维码图片
//如果覆盖图图片本身带透明色,则使用imagecopy方法
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//输出图片
//    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
//    switch ($dst_type) {
//        case 1://GIF
//            header('Content-Type: image/gif');
//            imagegif($dst);
//            break;
//        case 2://JPG
//            header('Content-Type: image/jpeg');
//            imagejpeg($dst);
//            break;
//        case 3://PNG
//            header('Content-Type: image/png');
//            imagepng($dst);
//            break;
//        default:
//            break;
//    }
     header("Content-type: image/png");
     imagepng($dst,'../uploads/user/'.$uid.'.png');//根据需要生成相应的图片
//    imagejpeg($dst,'../uploads/user/'.$uid.'.jpg');
    imagedestroy($dst);
    imagedestroy($src);

4.已完成,欢迎提出意见.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值