Use phpqrcode to generate QR code

It is quite difficult to generate a QR code using PHP language. Of course, except for calling the interface for generating QR code images (for example, the interface of Liantu http://www.liantu.com/), if you write your own code to generate it, Really no way to start. However, we can use phpqrcode, a ready-made class file, a PHP QR code generation library, which can easily generate QR codes.
Preliminary preparation:
1. Download the phpqrcode class file, download address: https://sourceforge.net/projects/phpqrcode/
2. The PHP environment must be enabled to support GD2 extension library support (in general, it is enabled)

Interpretation of the method:
The downloaded class file is a compressed package, which contains many files and demo programs. We only need the phpqrcode.php file inside to generate the QR code. It is a collection file of multiple classes, we need to use the png() method (line 3090) of the QRcode class (line 2963) inside:
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);
}
The first parameter $text: the content contained in the QR code, which can be a link, text, json string, etc.;
the second parameter $outfile: the default is false, no file is generated, only two The QR code image is returned to the output; otherwise, the file name and path for storing the generated QR code image need to be given;
the third parameter $level: The default value is L, and the values ​​that can be passed by this parameter are L(QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%), this parameter controls the fault tolerance rate of the QR code, different parameters indicate the percentage of the area that the QR code can be covered, that is, the coverage The area can also be identified;
the fourth parameter $size: control the size of the generated image, the default is 4;
the fifth parameter $margin: control the size of the blank area for generating the QR code;
The sixth parameter $saveandprint: save the QR code image and display it, $outfile must pass the image path;

Example of use:
generate QR code (generate image file)
// 1. Generate original QR code (generate image file)
function scerweima($url=''){
require_once 'phpqrcode.php';
$value = $url; //QR code content
$errorCorrectionLevel = 'L'; //Error tolerance level
$matrixPointSize = 5; //Generate image size
//Generate QR code image
$filename = 'qrcode/'.microtime().'.png' ;
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; //Original QR code image file generated
$QR = imagecreatefromstring(file_get_contents($QR));
//Output image
imagepng($QR, 'qrcode.png');
imagedestroy($QR);
return '<img src="qrcode.png" alt="Use WeChat to scan and pay">';
}

//The target image connection resource. $logo = imagecreatefromstring(file_get_contents($logo)); //Source image connection resource.
















$QR_width = imagesx($QR); //The width of the QR code image
$QR_height = imagesy($QR); //The height of the QR code image
$logo_width = imagesx($logo); //The width of the logo image
$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; //logo width scaling Ratio (width of itself/width after combination)
$logo_qr_height = $logo_height/$scale; //The height of the logo after the combination
$from_width = ($QR_width - $logo_qr_width) / 2; //The coordinate point where the upper left corner of the logo is located after the combination
/ /regroup and resize image
/*

  • imagecopyresampled() copies a square area from one image (source image) to another image
    */
    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $ logo_width, $logo_height);
    }
    //Output image
    imagepng($QR, 'qrcode.png');
    imagedestroy($QR);
    imagedestroy($logo);
    return '<img src="qrcode.png" alt="Use WeChat scan and pay">';
    }

//Call to view the result
echo scerweima1(' https://www.baidu.com ');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324552549&siteId=291194637
Recommended