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

A brief discussion on how ThinkPHP+phpqrcode generates QR codes

青灯夜游
Release: 2021-09-17 19:45:35
forward
3258 people have browsed it

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, '容错级别', '图片大小', '外边距离(白边)	');
Copy after login

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;
	}
}
?>
Copy after login

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!

Related labels:
source:掘金--元歌
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template