Generating QR code with logo in PHP

Keywords: QRCode PHP

#Generate QR code in PHP
(you need to download the phpqrcode class file)
Download address:
This class library is just a file. Open the file and you can see that it consists of multiple classes (I used 1.1.4). The first class name is qrstr, and the file name is phpqrcode.
After downloading the class library provided by the official website, you only need to use phpqrcode.php to generate QR code. Of course, your PHP environment must be enabled to support GD2.
phpqrcode.php provides a key png() method
I used the CI framework, so I changed the first class name qrstr to qrstr, phpqrcode.php to Qrcode.php, put the file in the application/libraries directory, and then loaded the class $this - > Load - > Library ('Qrcode ') in the CI controller file;
The code is as follows:

	png Several parameters of the method
	public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4, $saveandprint=false)
	The parameter $text means to generate two bit information text;
	The parameter $outfile indicates whether to output the QR code picture file. No by default;
	The parameter $level indicates the fault tolerance rate, i.e. the area covered can also be identified, which are l (QR green L, 7%), M (QR green L, 15%), Q (QR green L, 25%), H (QR green L, 30%);
	The parameter $size indicates the size of the generated image, which is 3 by default; the parameter $margin indicates the space between the blank areas around the QR code;
	The $saveandprint parameter indicates whether to save the QR code and display it.
    public function create_qrcode()
        $value = '';//QR code content
        $errorCorrectionLevel = 'H';  //Fault tolerance level
        $matrixPointSize = 6;      //Generate picture size

        //Generate QR code picture
        $path = "./Uploads/";//Directory of generated QR code
            mkdir($path, 0700,true);
        $time = 'admin/'.time().'.png';//Generated QR code file name
        $fileName = $path.$time;//1. Path of QR code file generated by assembly
        QRcode::png($value,$fileName , $errorCorrectionLevel, $matrixPointSize, 2);
        $logo = './images/xiaolu.png'; //logo picture ready
        $QR = $fileName;				//Generated original QR code picture file
        if (file_exists($logo)) {
            $QR = imagecreatefromstring(file_get_contents($QR));    //Target image connection resource.
            $logo = imagecreatefromstring(file_get_contents($logo));  //Source image connection resource.
            $QR_width = imagesx($QR);      //QR code picture width
            $QR_height = imagesy($QR);     //QR code image height
            $logo_width = imagesx($logo);    //logo picture width
            $logo_height = imagesy($logo);   //logo image height
            $logo_qr_width = $QR_width / 4;   //Width of logo after combination (1 / 5 of QR code)
            $scale = $logo_width/$logo_qr_width;  //Width scaling ratio of logo (own width / combined width)
            $logo_qr_height = $logo_height/$scale; //Height of logo after combination
            $from_width = ($QR_width - $logo_qr_width) / 2;  //Coordinate point of upper left corner of logo after combination
            //Recombine and resize pictures
             * imagecopyresampled() Copy a square area from one image (source image) to another
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        //Output pictures
        $time = 'admin/'.time().'.png';//Generated QR code file name
        $logo_img_path = $path.$time;
        imagepng($QR, $logo_img_path);
            //Generate operation log
            $qrcode = $this->qrcode_model->save($time);
                $content = 'Add QR code id by'.$qrcode;
                $result = array('ret'=>1);
                print json_encode($result);
                $result = array('ret'=>1,'msg'=>'Save failed');
                print json_encode($result);
            $result = array('ret'=>1,'msg'=>'New failure');
            print json_encode($result);

Posted by JaGeK on Fri, 29 Nov 2019 08:34:53 -0800