First, download the Phpqrcode and unzip it into the project Thinkphp\library\vendor directory.
Index_index.html (template can be configured by itself)
<formAction= "{: U (' Index/index ')}"enctype= "Multipart/form-data"Method= "POST"> <b>Size:</b> <inputtype= "text"name= "Matrixpointsize"> <BR> <b>Margin Size:</b> <Selectclass= "Form-control"name= "Matrixmarginsize" > <optionvalue= "1"selected= "Selected">1px</option> <optionvalue= "2">2px</option> <optionvalue= "3">3px</option> <optionvalue= "4">5px</option> </Select><BR> <b>Fault tolerance level:</b> <Selectclass= "Form-control"name= "Errorcorrectionlevel"ID= "Errorcorrectionlevel"> <optionvalue= "L">Low (7%)</option> <optionvalue= "M"selected= "Selected">Medium (15%)</option> <optionvalue= "Q">Quartile (25%)</option> <optionvalue= "H">High (30%)</option> </Select><BR> <b>Two-dimensional code content:</b> <inputtype= "text"name= "Content"> <BR> <b>Two-dimensional code logo:</b> <inputtype= "File"name= "Test" /><BR><BR> <inputtype= "Submit"value= "Generate two-dimensional code"></form>
Controller: IndexController.class.php
<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionindex () {$config=Array( ' MaxSize ' = 3145728, ' RootPath ' and '/upload/', ' savepath ' and ', ', ' Savename ' =Array(' uniqid ', '), ' exts ' =Array(' jpg ', ' gif ', ' PNG ', ' jpeg '), ' autosub ' =true, ' subname ' =Array(' Date ', ' YMD '), ); $upload=New\think\upload ($config);//instantiate an upload class//Upload a single file $info=$upload->uploadone ($_files[' Test ']); if(!$info) {//Upload error message $qrcode _path_new= './public/home/images/code '. ' _‘.Date("Ymdhis"). png; $content=$_post[' Content ']; $matrixPointSize=$_post[' Matrixpointsize ']; $matrixMarginSize=$_post[' Matrixmarginsize ']; $errorCorrectionLevel=$_post[' Errorcorrectionlevel ']; Makecode_no_pic ($content,$qrcode _path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel); $this->assign (' img ',$qrcode _path_new); }Else{ $qrcode _path= "./upload/".$info[' Savepath '].$info[' Savename ']; $content=$_post[' Content ']; $matrixPointSize=$_post[' Matrixpointsize ']; $matrixMarginSize=$_post[' Matrixmarginsize ']; $errorCorrectionLevel=$_post[' Errorcorrectionlevel ']; $url= "./upload/qrcode/".Date("Ymdhis"). ". png; Makecode ($qrcode _path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url); $this->assign (' img ',$url); } $this-display (); }}
Then create a new function.php under Project Application/home/common
<?PHPfunctionMakecode ($qrcode _path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url){ /** Parameter Details: * $qrcode _path:logo Address * $content: Need to generate two-dimensional code content * $matrixPointSize: two-dimensional code ruler Inch size * $matrixMarginSize: Generate a two-dimensional code margin * $errorCorrectionLevel: Fault tolerance level * $url: Generated with the logo of the two-dimensional code address * */ Ob_clean (); Vendor (' Phpqrcode.phpqrcode '); $object=New\qrcode (); $qrcode _path_new= './public/home/images/code '. ' _‘.Date("Ymdhis"). PNG ';//define the path and name of the generated QR code $object::p ng ($content,$qrcode _path_new,$errorCorrectionLevel,$matrixPointSize,$matrixMarginSize); $QR= Imagecreatefromstring (file_get_contents($qrcode _path_new));//imagecreatefromstring: Creating an image resource from the image stream in a string $logo= Imagecreatefromstring (file_get_contents($qrcode _path)); $QR _width= Imagesx ($QR);//Get image Width function $QR _height= Imagesy ($QR);//Get Image Height function $logo _width= Imagesx ($logo);//Get image Width function $logo _height= Imagesy ($logo);//Get Image Height function $logo _qr_width=$QR _width/4;//the width of the logo $scale=$logo _width/$logo _qr_width;//Calculate scale $logo _qr_height=$logo _height/$scale;//Calculate logo Height $from _width= ($QR _width-$logo _qr_width)/2;//Specify the coordinate position of the logoImagecopyresampled ($QR,$logo,$from _width,$from _width, 0, 0,$logo _qr_width,$logo _qr_height,$logo _width,$logo _height); /** imagecopyresampled (Resource $dst _image, resource $src _image, int $dst _x, int $dst _y, int $src _x, int $src _y, int $dst _w, int $dst _h, int $src _w, int $src _h) * Parameter details: * $DST _image: Target image connection resource. * $SRC _image: Source Image connection resource. * $DST _x: target x coordinate point. * $DST _y: target y coordinate point. * $SRC _x: The x-coordinate point of the source. * $SRC _y: The y-coordinate point of the source. * $DST _w: Target width. * $DST _h: target height. * $SRC _w: The width of the source image. * $SRC _h: The height of the source image. * */ Header("Content-type:image/png"); //$url: Define the address and name of the QR code that generated the logoImagepng ($QR,$url); } functionMakecode_no_pic ($content,$qrcode _path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel){ Ob_clean (); Vendor (' Phpqrcode.phpqrcode '); $object=New\qrcode (); $object::p ng ($content,$qrcode _path_new,$errorCorrectionLevel,$matrixPointSize,$matrixMarginSize); }
In the absence of picture upload, the generation of my QR code is only a simple two-dimensional code, only when there is a logo upload, will generate a logo with the QR code.
For example, the two-dimensional code generated when there is no Logo:
The QR code that is generated when a picture is uploaded:
case Source Download : Thinkphp_3.2.2.zip
pgpqrcode Download : Phpqrcode.rar
Thinkphp3.2 combined with Phpqrcode to generate two-dimensional code (including logo QR code), with case