TP5实现二维码的生成

二维码是使用非常广泛,本文用TP5实现二维码和logo的生成与设置及路径保存等

下载qr_code插件

  1. 下载 composer
  2. 利用 composer 下载 qr_code 插件
    composer require endroid/qr-code

我用的编译神器是PhpStorm ,所以在下面的 Terminal 中可以输入以上命令即可。
完成后可以在 Vender/endroid/qr-code 下找到安装好的文件。

代码撰写

生成简单的二维码图

最简单的看到二维码的代码如下,运行就可以在网页上看到,手机扫描,直接访问输入的网址。如下 index 控制器下的 Index.php

<?php
namespace app\index\controller;

use Endroid\QrCode\QrCode;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $qrCode = new QrCode('http://www.baidu.com');
        header('Content-Type: ' . $qrCode->getContentType());
        echo $qrCode->writeString();
        die;
    }
}

生成简单的二维码效果如下图所示:
二维码

生成复杂的二维码图

有时候使用二维码的时候,除了内容外,还是需要对二维码进行一定的设置,比如二维码的大小,颜色,边框等;还有 logo 的添加,logo 的大小等。二维码图片存放位置等。

前提:在使用一下代码前,保存二维码需要给二维码个路径(否则无法保存文件流),需要 logo 的话,还要提供 logo 的路径。

假如你已经提供了以上两个路径。下面就把生成二维码封装成了一个函数,代码如下:

index控制器下的Index.php :

<?php
namespace app\index\controller;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use think\Controller;

class Index extends Controller
{
    public function index1()
    {
        // 二维码内容
        $code_content = 'https://firmovebrick.github.io/';
        // 二维码名字
        $code_name = 'Pandada_code';
        // logo图片地址
        $code_logo = 'static\1.jpg';
        // logo的尺寸
        $code_logo_width = 40;
        // 返回二维码存放的地址
        $code_img = self::getQrCode($code_content, $code_name, 200, $code_logo, $code_logo_width);
        $data = [
            'code_img' => $code_img,
            'code_name' => $code_name,
        ];
        $this->assign('data', $data);
        return $this->fetch('index/index');
    }

    public static function getQrCode($code_content, $code_name, $code_size = 100, $code_logo = '', $code_logo_width = 20, $code_font = null)
    {
        // 二维码内容
        $qr_code = new QrCode($code_content);
        // 二维码设置
        $qr_code->setSize($code_size);
        // 边框宽度
        $qr_code->setMargin(10);
        // 图片格式
        $qr_code->setWriterByName('png');
        // 字符编码
        $qr_code->setEncoding('UTF-8');
        // 容错等级,分为L、M、Q、H四级
        $qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
//        $qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
        // 颜色设置,前景色,背景色(默认黑白)
        $qr_code->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qr_code->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        // 二维码标签
        if ($code_font) {
            $qr_code->setLabel('Scan the Code ', 16, __DIR__ . '字体地址', LabelAlignment::CENTER);
        }
        // logo设置
        if ($code_logo) {
            $qr_code->setLogoPath($code_logo);
            // logo大小
            $qr_code->setLogoWidth($code_logo_width);
            // 存放地址
            $code_path = 'static/qrcode/' . $code_name . '.png';
            $qr_code->writeFile($code_path);
        } else {
            // 存放地址
            $code_path = 'static/qrcode/' . $code_name . '.png';
            $qr_code->writeFile($code_path);
        }
        // 输出图片
        header('Content-Type: ' . $qr_code->getContentType());
        $qr_code->writeString();
        return $code_path;
    }
}

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>
<body>
<img src="{$data.code_img}" alt="">
<div>{$data.code_name}</div>
</body>
</html>

运行后就会在页面上显示自己的二维码。如下图:

在这里插入图片描述
在这里需要注意的是,为了方便小编把处理数据的数据写在了Controller里面,记得在实际开发项目中得把方法放在模型中,需要调用直接实例化模型对象就好了。

参数介绍

setSize                 -- 二维码大小 px
setWriterByName         -- 写入文件的后缀名
setMargin               -- 二维码内容相对于整张图片的外边距
setEncoding             -- 编码类型
setErrorCorrectionLevel -- 容错等级,分为L、M、Q、H四级
setForegroundColor      -- 前景色
setBackgroundColor      -- 背景色
setLabel                -- 二维码标签
setLogoPath             -- 二维码logo路径
setLogoWidth            -- 二维码logo大小 px
//背景颜色
setBackgroundColor      -- 背景色
setLabel                -- 二维码标签
setLogoPath             -- 二维码logo路径
setLogoWidth            -- 二维码logo大小 px

想要了解更多,大家可以查看这个网址:qr_code插件使用教程
小编的分享,TP5实现二维码的生成,到这里就结束了,希望对你有所帮助

猜你喜欢

转载自blog.csdn.net/weixin_44000238/article/details/106602688