How to generate a QR code under the thinkphp5 framework? (code)

不言
Release: 2023-04-04 08:32:01
forward
4301 people have browsed it

This article brings you how to generate a QR code under the thinkphp5 framework? (code), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Without further ado, let’s start with the code:

The first type: No need to save the file locally, display it directly on the front page:

This is the content in the controller, Oh, yes, first download the SDK: .phpqrcode class file download, download address: https://sourceforge.net/projects/phpqrcode/

Plug-in only needs: The downloaded class file is a compressed package, It contains many files and demonstration programs. We only need the phpqrcode.php file inside to generate a QR code. It is a collection file of multiple classes. We need to use the png() method (line 3090) of the QRcode class (line 2963) inside.

The file is placed in the framework extend file. You can name the folder yourself. Mine is PhpQrcode. The file inside is: phpqrcode.php

png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。 $imageString = base64_encode(ob_get_contents()); //关闭缓冲区 ob_end_clean(); //把生成的base64字符串返回给前端 $data = array( 'code'=>200, 'data'=>$imageString ); return json($data); } }
Copy after login

Front-end file: Of course, I use The jquery is relatively old, so you can modify it yourself if you use jquery:

二维码展示
Copy after login

The above is the detailed content of How to generate a QR code under the thinkphp5 framework? (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!