PHP 二维码生成+与活动图片合成

参考链接:https://blog.csdn.net/dream_dt/article/details/79667109
依赖库文件 phpqrcode.php
代码逻辑:
1.生成一张url相关的 二维码 QR
2.把log图片跟QR合并成一个带logo的二维码 last
3.把带logo的的二维码跟 活动图片合成为一张图 保存到本地 返回路径

public function getActivityImg($template,$url,$x,$y)
{
    //引入二维码类
    $phpqrcode = new \qrstr();
    //二维码中间添加logo
    $logo = "https://img.alicdn.com/imgextra/i1/3518866829/TB2i9WHfhSYBuNjSsphXXbGvVXa_!!3518866829.jpg";
    $QR = "base.png";
    $last = "last.png";
    $errorCorrectionLevel = 'Q'; //防错等级
    $matrixPointSize = 3; //二维码大小

    //生成二维码
    //参数内容:二维码储存内容,生成存储,防错等级,二维码大小,白边大小
    \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);

    //合并logo跟二维码-----------------start
    $QR = imagecreatefromstring(file_get_contents($QR));
    $logo = imagecreatefromstring(file_get_contents($logo));
    $QR_width = imagesx($QR);
    $logo_width = imagesx($logo);
    $logo_height = imagesy($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);
    imagepng($QR,$last); // 生成带log的二维码图片 存储到last
    //合并logo跟二维码-----------------end

    //合成带logo的二维码图片跟 模板图片--------------start
    $path_1 = $template;
    $path_2 = $last;
    $image_1 = imagecreatefrompng($path_1);
    $image_2 = imagecreatefrompng($path_2);
    $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
    $color = imagecolorallocate($image_3, 200, 200, 200);
    imagefill($image_3, 0, 0, $color);
    imageColorTransparent($image_3, $color);
    imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));

    imagecopymerge($image_3, $image_2, $x, $y,0, 0, imagesx($image_2), imagesy($image_2), 100);
    //合成带logo的二维码图片跟 模板图片--------------end

    //输出到本地文件夹
    $fileName=md5(basename($template).$url);
    $EchoPath='../public/code/'.$fileName.'.png';
    imagepng($image_3,$EchoPath);
    imagedestroy($image_3);
    //返回生成的路径
    print_r($EchoPath);
    return $EchoPath;
}

猜你喜欢

转载自blog.51cto.com/pilipala/2148890