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); } }
Front-end file: Of course, I use The jquery is relatively old, so you can modify it yourself if you use jquery:
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!