Home  >  Article  >  PHP Framework  >  A brief discussion on how ThinkPHP+phpqrcode generates QR codes

A brief discussion on how ThinkPHP+phpqrcode generates QR codes

青灯夜游
青灯夜游forward
2021-09-17 19:45:353179browse

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!

A brief discussion on how ThinkPHP+phpqrcode generates QR codes

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(&#39;&#39;);
		!isset($data[&#39;text&#39;]) && $this->error(&#39;参数非法&#39;);
		$text  = trim($data[&#39;text&#39;]); 
		//计算图片尺寸
		$width = isset($data[&#39;width&#39;]) ? trim($data[&#39;width&#39;]):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!

Statement:
This article is reproduced at:掘金--元歌. If there is any infringement, please contact admin@php.cn delete