Two words, first code:
In the first part: no need to save the file locally, and display directly on the foreground page:
This is the content in the controller. Oh, by the way, 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, which contains many files and demonstration programs. We only need the file phpqrcode.php in it to generate the QR code. It is a collection file of multiple classes. We need to use the png() method of QRcode class (line 2963) (line 3090).
The file can be named by itself in the frame extend file. Here is the file in PhpQrcode: phpqrcode.php
<?php namespace app\index\controller; use think\Controller; use think\Loader; Loader::import('PhpQrcode.phpqrcode',EXTEND_PATH,'.php'); class Index extends Controller { //ajax Visit //Generate QR code by linking public function code($url = "http://www.baidu.com") { $qrcode = new \QRcode(); // $qrimage = new \QRimage(); $value = $url; //QR code content $errorCorrectionLevel = 'H'; //Fault tolerance level $matrixPointSize = 6; //Generate picture size ob_start(); $qrcode::png($value,false , $errorCorrectionLevel, $matrixPointSize, 2); // $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); //This is to save the generated image stream from the buffer to the memory object, use base64 ﹐ encode to change it into an encoded string, and return it to the page through json. $imageString = base64_encode(ob_get_contents()); //Close buffer ob_end_clean(); //Generative base64 String returned to front end $data = array( 'code'=>200, 'data'=>$imageString ); return json($data); } }
Front end file: of course, the jquery I use is relatively old, so I can use jquery to change it by myself:
<div id="logos"> <button onclick="changess()">click</button> <img src="" class="qrcode" alt="QR code display"/> </div> <script type="text/javascript" src="__INDEX__js/jquery.js"></script> <script type="text/javascript"> function changess() { var logos = document.getElementById ('logos'); $.ajax({ type: "GET", url: "code.html", data: '', dataType: "json", success: function(r){ if (r.code==200) { //console.log(r); var path = 'data:image/png;base64,'+r.data; //to img Of sec Assignment. console.log(path); $("#logos").html("<img src="+path+">"); logos.html("<img src="+path+">"); console.log( logos.html("<img src="+path+">")); }else{ alert(r.err); } } }); } </script>
The effect is as follows:
The second method will be explained in the next article.