Home >Backend Development >PHP Tutorial >HP QR Code is a PHP QR code generation library
HP QR Code is a PHP QR code generation library, which can be used to easily generate QR codes. The official website provides downloads and multiple demonstration demos. View address:
http://phpqrcode.sourceforge.net/
Download provided by the official website After installing the class library, you only need to use phpqrcode.php to generate the QR code. Of course, your PHP environment must enable GD2 support.
phpqrcode.php provides a key png() method, in which the
parameter $text indicates the generation of two-digit information text;
parameter $outfile indicates whether to output a QR code image file, the default is no;
parameter $level indicates fault tolerance The rate, that is, the covered area can still be identified, are L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%);
Parameter $ size represents the size of the generated image, the default is 3; the parameter $margin represents the spacing value of the blank area around the border of the QR code; the parameter $saveandprint represents whether to save the QR code and display it.
. The code is as follows:
public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4, $saveandprint=false)
{
$enc = QRecode::factory( $level, $size, $margin);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}
Calling PHP QR Code is very simple. The following code can generate a piece of content: "http://www.cnblogs.com/txw1958/" QR code.
include 'phpqrcode.php';
QRcode::png('http://www.cnblogs.com/txw1958/');
In actual application, we will add our own LOGO in the middle of the QR code to enhance the publicity effect. So how to generate a QR code containing a logo? In fact, the principle is very simple. First use PHP QR Code to generate a QR code image, and then use PHP's image related function to add the pre-prepared logo image to the middle of the original QR code image just generated, and then regenerate a new one. QR code picture.
. The code is as follows:
include 'phpqrcode.php';
$value = 'http://www.cnblogs.com/txw1958/'; //QR code content
$errorCorrectionLevel = 'L';//Error tolerance level
$matrixPointSize = 6;//Generate image size
//Generate QR code image
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo. png';//Prepared logo image
$QR = 'qrcode.png';//Generated original QR code image
if ($logo !== FALSE) {
$QR = imagecreatefromstring(file_get_contents($ QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//Width of QR code image
$QR_height = imagesy($QR);//QR code image Height
$logo_width = imagesx($logo);//logo image width
$logo_height = imagesy($logo);//logo image height
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_wid th;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//Reassemble the image and resize it
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $ logo_qr_width,
$ logo_qr_height, $ logo_width, $ logo_height); T; t; "HelloWeixin.png" & GT ;';
include 'phpqrcode.php';
$value = 'http://www.cnblogs.com/txw1958/'; //QR code content
$errorCorrectionLevel = 'L';//Error tolerance level
$ matrixPointSize = 6;//Generate image size
//Generate QR code image
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png' ;//Prepared logo image
$QR = 'qrcode.png';//Generated original QR code image
if ($logo !== FALSE) {
$QR = imagecreatefromstring(file_get_contents($QR) );
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//Width of QR code image
$QR_height = imagesy($QR);//Height of QR code image
$logo_width = imagesx($logo);//Logo image width
$logo_height = imagesy($logo);//Logo image height
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$ logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//Reassemble the image and resize it
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//Output image
imagepng($QR, 'helloweixin.png');
echo '' ;
Because QR codes allow a certain degree of fault tolerance, general QR codes can still be decoded even if they are partially covered. Often when we scan a QR code, we can decode the scan result even less than halfway through. This is This is because the generator will repeatedly represent part of the information to improve its fault tolerance. This is why adding a LOGO image in the middle of the QR code does not affect the decoding results.
The above introduces that HP QR Code is a PHP QR code generation class library, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.