1. Download the phpqrcode extension library
Official download address: https://sourceforge.net/projects/phpqrcode/files/
Second, use the phpqrcode extension library
1. After decompression, open the following picture:
2. In order to facilitate the call, we can modify the file name of phpqrcode.php to " QRcode.php ", and then add the namespace, as follows:
3. Put the phpqrcode folder in the extend directory
4. Call in code
//引用
use phpqrcode\QRcode;
//调用类库静态方法
$qrcode=QRcode::png('二维码内容',false, '容错级别', '图片大小', '外边距离(白边) ');
5. Example
<?php
namespace app\index\controller;
use think\Controller;
use phpqrcode\QRcode;
class Qr extends Controller
{
/**
* 生成二维码接口
*/
public function api(){
$data=input('');
!isset($data['text']) && $this->error('参数非法');
$text = trim($data['text']);
//计算图片尺寸
$width = isset($data['width']) ? trim($data['width']):100;
$size = floor($width/37*100)/100 + 0.01;
$errorCorrectionLevel =intval(2) ;//容错级别
$matrixPointSize = intval($size); //生成图片大小
$margin =0;//外边距离(白边)
$qrcode=QRcode::png($text,false, $errorCorrectionLevel, $matrixPointSize, $margin);
die;
}
}
?>