Apart from anything else, the first on the code:
In the first: no need to save the file locally, directly on the front page display:
This is what the controller inside, oh, yes, we must first download the SDK: .phpqrcode class file download, download address: https: //sourceforge.net/projects/phpqrcode/
As long as the plug: class file downloaded is a compressed package offer, which contains many files and demo program, we only need a file inside phpqrcode.php it can generate a two-dimensional code. It is a collection of file more than one class, we need to use inside QRcode class (2963 lines) is png () method (line 3090).
Framework document on the document can extend itself to the folder a name, here is my PhpQrcode inside the file is: phpqrcode.php
<PHP? namespace App \ index \ the Controller; use of Think \ the Controller; use of Think \ Loader; Loader :: Import ( 'PhpQrcode.phpqrcode', EXTEND_PATH, '. PHP'); class the extends the Controller Index { // ajax access // generating a two-dimensional code by linking public function code ($ URL = "http://www.baidu.com") { $ = qrcode new new \ qRcode (); // $ = qrimage new new \ qRimage (); $ value = $ url; // dimensional code content $ errorCorrectionLevel = 'H'; // fault tolerance level $ matrixPointSize = 6; // generate picture size ob_start (); $ :: qrcode PNG ($ value, to false, errorCorrectionLevel $, $ matrixPointSize, 2); // $ object-> png ($ url , false, $ errorCorrectionLevel, $ matrixPointSize, 2); // this is to save the generated images from the stream buffer to the memory object, using base64_encode into coded strings via json return to the page. $ imageString = base64_encode (ob_get_contents () ); // Close buffer ob_end_clean (); // return the string to the front end base64 generated $ data = array ( 'code' => 200, 'data' => $ imageString) ; return JSON ($ Data); } }
Front file: Of course, I use jquery relatively old, it's about self-change of useful jquery Well:
<div id="logos"> <button onclick="changess()">点击</button> <img src="" class="qrcode" alt="二维码展示"/> </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); assignment to the img sec.var path = 'data: image / 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>
As the effect of:
Original Address: https: //www.cnblogs.com/devilgod/p/9699446.html