thinkphp uses phpqrcode to generate a QR code

Download third-party files

Here is a link to a cloud disk, extraction code: udzr

https://pan.baidu.com/s/1P8AvJLACI0NcP2IcP6YQBA

Compress the file to the vendor folder of the project.

Write method in public file. (Note: common.php under app)

function createQRcode($url,$flag=0){
    vendor("phpqrcode.phpqrcode");
    // 纠错级别:L、M、Q、H
    $level = 'L';
    // 点的大小:1到10,用于手机端4就可以了
    $size = 4;
    if($flag){
        $path = "Public/QRcode/";
        if(!file_exists($path)){
            mkdir($path, 0700,true);
        }
        // 生成的文件名
        $fileName = $path.time().'.png';//时间戳命名
        QRcode::png($url, $fileName, $level, $size);
        return $fileName;
    }else{
        QRcode::png($url, false, $level, $size);//不保存,直接显示二维码
        exit;
    }
}

Two methods of displaying QR code are used here

1- Called in the controller and then rendered to the view

namespace app\index\controller;

use think\Controller;
class Index extends Controller
{
    public function index()
    {
        $fileName = createQRcode("https://www.baidu.com",1);
        $this->assign('path',$fileName);
        return $this->fetch();
    }
}

In view

<img src="{$path}" />

2- Call the method directly in the view

<a href="{:createQRcode('https://www.baidu.com','1')}">百度</a>

Guess you like

Origin blog.csdn.net/hgb24660/article/details/100105764