一、下载phpqrcode扩展库
官方下载地址:https://sourceforge.net/projects/phpqrcode/files/
二、使用phpqrcode扩展库
1、解压后,打开如下图:
2、为了方便调用,我们可以修改phpqrcode.php这个文件名,修改成“QRcode.php”,然后添加命名空间,如下:
3、把phpqrcode文件夹放到extend扩展目录
4、在代码里调用
-
//引用
-
use
phpqrcode\
QRcode;
-
//调用类库静态方法
-
$qrcode=QRcode::png(
'二维码内容',
false,
'容错级别',
'图片大小',
'外边距离(白边) ');
5、示例
-
<?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;
-
}
-
}
-
?>
转载:https://blog.csdn.net/qq15577969/article/details/114216857
查看评论