tp5 thinkphp 使用phpqrcode生成二维码

版权声明:本人原创文章,转载时请保留所有权并以超链接形式标明文章出处 https://blog.csdn.net/qq_37138818/article/details/82623511

1--下载类库

composer require aferrandini/phpqrcode -vvv

2-common 的方法

/**
 * 功能:生成二维码
 * @param string $qrData 手机扫描后要跳转的网址
 * @param string $qrLevel 默认纠错比例 分为L、M、Q、H四个等级,H代表最高纠错能力
 * @param string $qrSize 二维码图大小,1-10可选,数字越大图片尺寸越大
 * @param string $savePath 图片存储路径
 * @param string $savePrefix 图片名称前缀
 */
function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = 'qrcode')
{
    if (!isset($savePath)) return '';
    //设置生成png图片的路径
    $PNG_TEMP_DIR = $savePath;

    //检测并创建生成文件夹
    if (!file_exists($PNG_TEMP_DIR)) {
        mkdir($PNG_TEMP_DIR);
    }
    $filename = $PNG_TEMP_DIR . 'test.png';
    $errorCorrectionLevel = 'L';
    if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
        $errorCorrectionLevel = $qrLevel;
    }
    $matrixPointSize = 4;
    if (isset($qrSize)) {
        $matrixPointSize = min(max((int)$qrSize, 1), 10);
    }
    if (isset($qrData)) {
        if (trim($qrData) == '') {
            die('data cannot be empty!');
        }
        //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
        $filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
        //开始生成
        \PHPQRCode\QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    } else {
        //默认生成
        \PHPQRCode\QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    if (file_exists($PNG_TEMP_DIR . basename($filename)))
        return basename($filename);
    else
        return FALSE;
}

3--控制器的 方法

public function test(){
            //$savePath 图片存储路径
            $savePath = APP_PATH . '/../Public/qrcode/';
            //路径
            $webPath = '/public/qrcode/';
            //$qrData 手机扫描后要跳转的网址
            $qrData = 'http://www.baidu.com';
            // $qrLevel 默认纠错比例 分为L、M、Q、H四个等级,H代表最高纠错能力
            $qrLevel = 'H';
            //$qrSize 二维码图大小,1-10可选,数字越大图片尺寸越大
            $qrSize = '8';
            // $savePrefix 图片名称前缀
            $savePrefix = 'NickBai';
            if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
                $pic = $webPath . $filename;
            }
           echo "<img src='$pic'>";
    }

如整合期间有遇到什么问题 可以加群 858507220 一起讨论哦。

猜你喜欢

转载自blog.csdn.net/qq_37138818/article/details/82623511