How to generate QR code in ThinkPHP? The following article will introduce to you how ThinkPHP uses the phpqrcode extension library to generate QR codes. I hope it will be helpful to you!
1. Download the phpqrcode extension library
Official download address: https://sourceforge.net /projects/phpqrcode/files/
[Related tutorial recommendations: thinkphp framework]
2. Use the phpqrcode extension library
1. After decompression, open the following picture:
2. In order to facilitate the call, we can modify phpqrcode Change the file name .php to "QRcode.php", and then add the namespace, as follows:
3. Put the phpqrcode folder into extend extension directory
4. Call
//引用 use phpqrcode\QRcode; //调用类库静态方法 $qrcode=QRcode::png('二维码内容',false, '容错级别', '图片大小', '外边距离(白边) ');
5 in the code. 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; } } ?>
Original address: https:/ /juejin.cn/post/6986282985829957669
Author: Yuan Ge
Recommended study: "PHP Video Tutorial"
The above is the detailed content of A brief discussion on how ThinkPHP+phpqrcode generates QR codes. For more information, please follow other related articles on the PHP Chinese website!