Using phpqrcode two-dimensional code to generate class library and imagecopymerge function to make mosaic pictures

Posted by laanes on Sun, 08 Dec 2019 00:19:57 +0100

preparation in advance

1. Generate QR code with phpqrcode:

Principle analysis:

The downloaded class file is a compressed package, which contains many files and demo programs. We only need phpqrcode.php inside

This file can generate QR code. It is a collection file of multiple classes. We need to use the png() method of QRcode class in it:

public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) 
        {
            $enc = QRencode::factory($level, $size, $margin);
            return $enc->encodePNG($text, $outfile, $saveandprint=false);
        }
    /**
     * 
     * Parameter detailed explanation
     * The first parameter $text: the content of the QR code can be links, text, json strings, etc;
     * Second parameter $outfile:The default value is false, no file is generated, only the QR code picture is returned to the outputï¼›otherwiseThe file name and path to store the generated QR code image shall be given;
     * The third parameter $level: fault tolerance level, L by default.
     *      The transitive values were L (7%), m (15%), q (25%) and H (30%).
     *      This parameter controls the error tolerance rate of QR code. Different parameters represent the percentage of area that can be covered by QR code, that is, the covered area can also be identified
     * The fourth parameter $size: controls the size of the generated image. The default value is 4;
     * The fifth parameter $margin: controls the size of the blank area of the generated QR code;
     * The sixth parameter $save and print: save the QR code image and display it. The $outfile must pass the image path;
     * 
     */

Use example:

require_once("./phpqrcode.php");
function creatQrcode(){
    //Set the link address of the QR code
    $url = "http://www.baidu.com";
    //Set the tolerance level of QR code
    /*
     * Fault tolerance level: the higher the percentage of fault tolerance level, the easier to identify. Fault tolerance level:
     * Order by effect: H - > Q - > m - > L
     */
    $errorCorrectionLevel = 'H';    
    //Set the size of the generated QR code picture
    $matrixPointSize = 7;
    //Set the picture name of the generated QR code (path name depends on the project requirements)
    $filename = "test.png";
    QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);
    //The QR code has been generated above test.png)
}
creatQrcode();

2. If you want to place a logo in the middle of the generated QR Code:

<?php
//Introduce phpqrcode Class library
require_once("./phpqrcode.php");
function creatQrcode(){
    //Set the link address of the QR code
    $url = "http://www.baidu.com";
    //Set the tolerance level of QR code
    /*
     * Fault tolerance level: the higher the percentage of fault tolerance level, the easier to identify. Fault tolerance level:
     * Order by effect: H - > Q - > m - > L
     */
    $errorCorrectionLevel = 'H';    
    //Set the size of the generated QR code picture
    $matrixPointSize = 7;
    //Set the picture name of the generated QR code
    $filename = "test.png";
    QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);
    //The QR code has been generated above test.png)
    $logo = './img/logo.png';
    $QR = $filename;
    $QRlogo = './img/qrlogo.png';
    if(file_exists($logo)){
        // Function: imagecreatefromstring(): Create a canvas and create a new image from the image stream in the string
        $QR = imagecreatefromstring(file_get_contents($QR));        //Target image connection resource.
        $logo = imagecreatefromstring(file_get_contents($logo));     //Source image connection resource.
        // php function: imagesx(resource image): get image width
        // PHP function: image (resource image): get image height
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);
        $logo_width = imagesx($logo);//logo image width 
        $logo_height = imagesy($logo);//logo Picture height 

        $logo_qr_width = $QR_width / 5;   //After combination logo Width(1% of QR code/5)
        $scale = $logo_width/$logo_qr_width;  //logo Width scaling ratio of(Width of itself/Width after combination)
        $logo_qr_height = $logo_height/$scale; //After combination logo Height
        $from_width = ($QR_width - $logo_qr_width) / 2;  //After combination logo Coordinate point of upper left corner

        //Recombine and resize pictures
        /**
         * Function imagecopydisplayed(): copies a square area of an image to another image, smoothly inserts the pixel value, so, in particular, reduces the size of the image while still maintaining a great sharpness. Parameter detailed explanation
         *
         * bool 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 )
         *
         * 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 X coordinate point of the source.
         * src_y 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.
         */
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        // PHP function:imagepng ( resource image [, string filename] ):with PNG Format output image to browser or file
        imagepng($QR,$QRlogo);
        echo '<image src="'.$QRlogo.'"/>';
    }
}
creatQrcode();

3. If the QR code with logo is to be placed on a poster:

The image copy merge function makes the mosaic (mosaic, merge) picture also suitable for watermark and so on

<?php
//Introduce phpqrcode Class library
require_once("./phpqrcode.php");
function creatQrcode(){
    //Set the link address of the QR code
    $url = "http://www.baidu.com";
    //Set the tolerance level of QR code
    /*
     * Fault tolerance level: the higher the percentage of fault tolerance level, the easier to identify. Fault tolerance level:
     * Order by effect: H - > Q - > m - > L
     */
    $errorCorrectionLevel = 'H';    
    //Set the size of the generated QR code picture
    $matrixPointSize = 7;
    //Set the picture name of the generated QR code
    $filename = "test.png";
    QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);
    //The QR code has been generated above test.png)

    //--------------------------------
    //Add logo on QR code
    $logo = './img/logo.png';
    $QR = $filename;
    $QRlogo = './img/qrlogo.png';
    if(file_exists($logo)){
        // Function: imagecreatefromstring(): Create a canvas and create a new image from the image stream in the string
        $QR = imagecreatefromstring(file_get_contents($QR));        //Target image connection resource.
        $logo = imagecreatefromstring(file_get_contents($logo));     //Source image connection resource.
        // php function: imagesx(resource image): get image width
        // PHP function: image (resource image): get image height
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);
        $logo_width = imagesx($logo);//logo image width 
        $logo_height = imagesy($logo);//logo Picture height 

        $logo_qr_width = $QR_width / 5;   //After combination logo Width(1% of QR code/5)
        $scale = $logo_width/$logo_qr_width;  //logo Width scaling ratio of(Width of itself/Width after combination)
        $logo_qr_height = $logo_height/$scale; //After combination logo Height
        $from_width = ($QR_width - $logo_qr_width) / 2;  //After combination logo Coordinate point of upper left corner

        //Recombine and resize pictures
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        // PHP function:imagepng ( resource image [, string filename] ):with PNG Format output image to browser or file
        imagepng($QR,$QRlogo);



        //---------------------
        // The two-dimensional code with logo is placed on the background map like poster

        $dst_path = './img/xuexi.jpg';     //Base map
        $src_path = $QRlogo;     //Overlay, let's continue with the one above QRlogo

        //Create picture instance
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //Get the width and height of the overlay
        list($src_w, $src_h) = getimagesize($src_path);
        /**
         *
         * PHP Function: imagecopymerge()/imagecopy()
         *
         * bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
         * The coordinates in the image are copied from SRC X and Src y, with width of SRC W and height of SRC h to the positions of DST X and DST y in the image. The merging degree of the two images will be determined according to the pct, and the value range is from 0 to 100. When pct = 0, nothing is actually done. When it is 100, this function is exactly the same as imagecopy() for palette image, which implements alpha transparency for true color image.
         *
         */
        imagecopymerge($dst, $src, 20, 120, 0, 0, $src_w, $src_h, 100);
        list($dst_w, $dst_h) = getimagesize($dst_path);
        imagepng($dst,'./img/aaa.png');
        imagedestroy($dst);
        imagedestroy($src);
    }
}
creatQrcode();

(Note: many references are available on the Internet. If there is any error, please leave a message for correction!)

Topics: PHP QRCode JSON