Home > Backend Development > PHP Tutorial > Encapsulation of QR code generation in TP5

Encapsulation of QR code generation in TP5

*文
Release: 2023-03-18 08:58:01
Original
5070 people have browsed it

Nowadays, QR codes are widely used, and some websites also have the need to generate QR codes. This article uses TP5 as an example to encapsulate the function of generating a QR code.

1. Download the QR code plug-in Phpqrcode at https://sourceforge.net/projects/phpqrcode/files/, and put the downloaded folder under \thinkphp\vendor


2. Add the function qrcode() in the controller (or in the public function library);


  /**
     * 制作二维码图片
     * @return [type] [description]
     */
    public function qrcode() {
    //加载第三方类库
        vendor('phpqrcode.phpqrcode');
        $url="http://finance.china.com.cn/hz/gn/2345/20171127/19523.shtml";
        $size=4;    //图片大小
        $errorCorrectionLevel = "Q"; // 容错级别:L、M、Q、H
        $matrixPointSize = "8"; // 点的大小:1到10
        //实例化
        $qr = new \QRcode();
        //会清除缓冲区的内容,并将缓冲区关闭,但不会输出内容。
        ob_end_clean();
    //输入二维码
        $qr::png($url, false, $errorCorrectionLevel, $matrixPointSize);
               
    }
Copy after login
参数说明:
/*
* png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 4, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
* 参数说明:
* $text 就是url参数
* $outfile 默认否,不生成文件,只返回二维码图片,否则需要给出保存路径
* $level 二维码容错率,默认L(7%)、M(15%)、Q(25%)、H(30%)
* $size 二维码图片大小,默认4
* $margin 二维码空白区域大小
* $saveabdprint 二维码保存并显示,$outfile必须传路径
* $back_color 背景颜色
* $fore_color 绘制二维码的颜色
* tip:颜色必须传16进制的色值,并把“#”替换为“0x”; 如 #FFFFFF => 0xFFFFFF
*/
Copy after login


3. QR code generation and calling. For example: website domain name /index/index/qrcode, you can display the QR code.

Related reading:

How to get openid after scanning the QR code image generated by PHP?

Yii2.0 framework generates QR code function implementation code

## Use Thinkphp3.2 combined with phpqrcode to generate 2D code code

The above is the detailed content of Encapsulation of QR code generation in TP5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template