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

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

public function index(){

    $uid = $this->auth->id;
    $appId = $GLOBALS['app_id'];
    $code = db("user_" . $appId)->field('invite_code,jointime')->where("id", $uid)->find();
    //用户根据分享邀请码和分享海报背景图合并
    $img = explode(",", 多张图片分割为数组);
    $url = '二维码跳转链接';
    $x = "293";
    $y = "1031";
    foreach ($img as $v) {
        $template = "http://" . $serverName . $v . "";
        $fileName = md5(basename($template) . $url);
        $time = date("Ym", $code['jointime']);
        $file = '../public/uploads/' . $appId . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.jpg'; //压缩后图片
        $fileSrc = "http://" . $serverName . substr($file, 9);

        if (file_exists($file)) {  //文件存在返回图片路径
            $time = time();
            $img_time = filemtime($file);
            if ($time - $img_time < 86400) {
                $list['image'][] = $fileSrc;
            } else {
                unlink($file);
                $resImg = $this->getActivityImg($template, $url, $x, $y, $code['invite_code'], $code['jointime'], $uid);
                $list['image'][] = "http://" . $serverName . substr($resImg, 9);
            }
        } else { //在线生成图片
            $resImg = $this->getActivityImg($template, $url, $x, $y, $code['invite_code'], $code['jointime'], $uid);
            $list['image'][] = "http://" . $serverName . substr($resImg, 9);
        }
    }
    if (empty($list)) {
        $this->error('查询数据为空', []);
    } else {
        $this->success('数据返回成功', $list);
    }
}

//参数 活动模板图片,二维码url,模板内二维码的位置
private function getActivityImg($template, $url, $x, $y, $code, $jointime, $uid) {
    $time = date("Ym", $jointime);
    $file = $GLOBALS['app_id'];
    $md5 = md5(basename($template) . $url);
    //创建文件夹
    $dir = iconv("UTF-8", "GBK", "../public/uploads/" . $file . "/invite/" . $time . "");
    if (!file_exists($dir)) {
        mk_dir($dir, 0777, true);
    }
    //引入二维码类
    new \qrstr();
    //$QR = "../public/uploads/$file/base.png";
    $QR = "../public/uploads/$file/invite/$time/" . $uid . "_" . $md5 . "_base.png";
    $errorCorrectionLevel = 'Q'; //防错等级
    $matrixPointSize = 5; //二维码大小

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

    //合成带logo的二维码图片跟 模板图片--------------start
    $path_1 = $template;
    $path_2 = $QR;
    $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);

    //在图片上加文字
    $font_file = ROOT_PATH . "public/assets/fonts/msyh.ttc"; //字体文件
    //$font_color_1 = ImageColorAllocate($image_1, 100, 100, 100);
    $font_color_1 = ImageColorAllocate($image_1, 255, 255, 255);
    imagettftext($image_1, 20, 0, 306, 1000, $font_color_1, $font_file, "邀请码:" . $code); //邀请码
    imageline($image_1, 500, 500, 500, 500, $font_color_1);
    //在图片上加文字end

    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/uploads/' . $file . '/' . $fileName . '.png';
    $source = '../public/uploads/' . $file . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.png'; //原图
    $EchoPath = '../public/uploads/' . $file . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.jpg'; //压缩后图片

    imagepng($image_3, $source);
    //imagepng($image_3, $EchoPath);
    //$percent = 1;
    //$image = (new imgcompress($source,$percent))->compressImg($EchoPath);
    $this->handlePic($source);
    imagedestroy($image_3);

    unlink($source);
    unlink($QR);

    //返回生成的路径
    return $EchoPath;
}

    /**
 * 图片压缩
 * @param $srcPathName
 */
public function handlePic($srcPathName)
{

    $srcFile = $srcPathName;
    $srcFileExt = strtolower(trim(substr(strrchr($srcFile, '.'), 1)));
    if ($srcFileExt == 'png') {
        $dstFile = str_replace('.png', '.jpg', $srcPathName);
        $photoSize = GetImageSize($srcFile);
        $pw = $photoSize[0];
        $ph = $photoSize[1];
        $dstImage = ImageCreateTrueColor($pw, $ph);
        imagecolorallocate($dstImage, 255, 255, 255);
        //读取图片
        $srcImage = ImageCreateFromPNG($srcFile);
        //合拼图片
        imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $pw, $ph, $pw, $ph);
        imagejpeg($dstImage, $dstFile, 70);
        imagedestroy($srcImage);
    }
}

转载于:https://blog.51cto.com/pilipala/2148890

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值