Thinkphp3.2 combined with Phpqrcode to generate two-dimensional code (including logo QR code), with case

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.