飞道的博客

Tinkphp使用phpqrcode扩展库生成二维码

294人阅读  评论(0)

一、下载phpqrcode扩展库

官方下载地址:https://sourceforge.net/projects/phpqrcode/files/

二、使用phpqrcode扩展库

1、解压后,打开如下图:

2、为了方便调用,我们可以修改phpqrcode.php这个文件名,修改成“QRcode.php”,然后添加命名空间,如下:

3、把phpqrcode文件夹放到extend扩展目录

 

4、在代码里调用


  
  1. //引用
  2. use phpqrcode\ QRcode;
  3. //调用类库静态方法
  4. $qrcode=QRcode::png( '二维码内容', false, '容错级别', '图片大小', '外边距离(白边) ');

5、示例 


  
  1. <?php
  2. namespace app\ index\ controller;
  3. use think\ Controller;
  4. use phpqrcode\ QRcode;
  5. class Qr extends Controller
  6. {
  7. /**
  8. * 生成二维码接口
  9. */
  10. public function api(){
  11. $data=input( '');
  12. ! isset($data[ 'text']) && $this->error( '参数非法');
  13. $text = trim($data[ 'text']);
  14. //计算图片尺寸
  15. $width = isset($data[ 'width']) ? trim($data[ 'width']): 100;
  16. $size = floor($width/ 37* 100)/ 100 + 0.01;
  17. $errorCorrectionLevel =intval( 2) ; //容错级别
  18. $matrixPointSize = intval($size); //生成图片大小
  19. $margin = 0; //外边距离(白边)
  20. $qrcode=QRcode::png($text, false, $errorCorrectionLevel, $matrixPointSize, $margin);
  21. die;
  22. }
  23. }
  24. ?>

 


转载:https://blog.csdn.net/qq15577969/article/details/114216857
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场