tp5 生成二维码并与背景图合并

10 篇文章 0 订阅

1.安装phpqrcode,命令:composer require endroid/qrcode     如果已安装无需重复安装

2.控制器中引用:use Endroid\QrCode\QrCode

3.查看自己PHP版本:我的是7.3,因为版本不同,使用方法可能有区别,具体看官方文档

4.我的要求是生成带邀请码的分享页面,步骤及代码如下:

  1. 步骤一:先用qrcode生成二维码,放到目录下
  2. 步骤二:二维码与背景图合并,然后替换上一步生成的二维码
  3. 步骤三:返回生成的图片地址
/**
     * 生成二维码
     */
    public function build()
    {
        $user_id = $this->auth->id;
        if (empty($user_id)) $this->error('请登录');
        $userInfo = User::where('id', $user_id)->field('id,code')->find();
        $url = Config('url_domain_root') . '?code = ' . $userInfo->code;
        //  生成二维码
        $writer = new PngWriter();
        $qrCode = QrCode::create($url)
            ->setEncoding(new Encoding('UTF-8'))
            ->setSize(300)
            ->setMargin(2)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        $result = $writer->write($qrCode);
        //  二维码保存到目录下
        $code_path = ROOT_PATH . 'public/code/' . $user_id . '_code.png';
        $result->saveToFile($code_path);
        //  与背景图合并
        $dst_path = ROOT_PATH . 'public/share.png'; //背景图片路径
        $src_path = $code_path;//覆盖图
        //创建图片的实例
        $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, 220, 900, 0, 0, $src_w, $src_h, 80);
        //如果覆盖图图片本身带透明色,则使用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");
        $res = imagepng($dst, $code_path);//根据需要生成相应的图片
        imagedestroy($dst);
        imagedestroy($src);
        $returnImg = Config('url_domain_root') . 'uploads/code/' . $user_id . '_code.png';
        if ($res) $this->success('获取成功!', $returnImg);
        $this->error('再试一次!');
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
, file_header.bfOffBits, SEEK_SET); uint8_t* p = *data + row_size * (*height - 1); for (int i = 0; i < *height; ++i) { fread(p, row_size, 1,在TP5中使用PHP QR Code库生成二维码时,可以通过`base64_encode()`将二维码图片转换 fp); p -= row_size; } fclose(fp); } void sobel_edge_detection(uint8_t* data, int width,为base64编码的字符串,然后通过接口返回给前端。 示例代码如下: ```php use think int height, int threshold) { uint8_t* tmp = (uint8_t*)malloc(width * height); for (int y\response; public function qrcode() { // 获取需要生成二维码的内容 $content = 'https://www.example = 1; y < height - 1; ++y) { for (int x = 1; x < width - .com'; // 设置二维码参数 $size = 10; $margin = 1; $errorCorrectionLevel1; ++x) { int gx = -data[(y - 1) * width * 3 + (x - 1 = 'H'; // 生成二维码图片对象 $qrCode = new QrCode($content); $qr) * 3] + data[(y - 1) * width * 3 + (x + 1) * 3] Code->setSize($size); $qrCode->setMargin($margin); $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel($ -2 * data[y * width * 3 + (x - 1) * 3] + 2 * data[y *errorCorrectionLevel)); // 将二维码图片转换为二进制字符串 $imageData = $qrCode->write width * 3 + (x + 1) * 3] -data[(y + 1) * width * 3String(); // 将二进制字符串转换为base64编码的字符串 $base64 = base64_encode($image + (x - 1) * 3] + data[(y + 1) * width * 3 + (x + Data); // 返回base64编码的字符串 return Response::create(['qrcode' => $base64], 'json'); 1) * 3]; int gy = -data[(y - 1) * width * 3 + (x - 1} ``` 在上述示例代码中,我们使用PHP QR Code库生成二维码图片对象,并将其转换为) * 3] - 2 * data[(y - 1) * width * 3 + x * 3] -二进制字符串,然后再通过`base64_encode()`函数将其转换为base64编码的字符串。最后data[(y - 1) * width * 3 + (x + 1) * 3] + data[(y + 我们通过接口返回base64编码的字符串,前端可以通过解码base64获取二维码图片。 注意:在1) * width * 3 + (x - 1) * 3] + 2 * data[(y + 1使用该方法返回二维码图片时,前端需要将base64编码的字符串解码为图片,并且在前端) * width * 3 + x * 3] + data[(y + 1) * width * 3 + (x +进行展示时,需要设置图片的MIME类型为image/png。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值