首先需要下载一个phpqrcode,这是一个PHP的二维码生成类库,下载地址:https://sourceforge.net/projects/phpqrcode/files/
你也可以composer下载:
composer require aferrandini/phpqrcode
如果是手动下载的,将文件放到vendor目录下
生成二维码方法:
function qrcode_save($url, $picPath, $level = 3, $size = 4)
{
Vendor('phpqrcode.phpqrcode');
$errorCorrectionLevel = intval($level); //容错级别
$matrixPointSize = intval($size); //生成图片大小
if (!file_exists(dirname($picPath))) {
mkdir(dirname($picPath));
}
//生成二维码图片
$object = new \QRcode();
$object->png($url, $picPath, $errorCorrectionLevel, $matrixPointSize, 2, true);
}
调用该方法
public function qrcode_show()
{
$name = ''; //图片的变量名
$url = ""; //跳转路径
$savePath = ROOT_PATH . 'public' . DS . 'upload'.DS .'QRcode'.DS.$name . 'max.jpg';
if (!file_exists($savePath)) {
$this->qrcode_save($url, $savePath, 3, 9);
}
$this->success('获取成功',$route);
}
ok 一个简单的二维码就生成成功了。