HPQRCode is a php qr code generation Class Library

Source: Internet
Author: User
: This article mainly introduces HPQRCode, which is a class library for generating php qr codes. if you are interested in PHP tutorials, refer to it. Hp qr Code is a php qr Code generation class library that allows you to easily generate QR codes. the official website provides download and multiple demo demos:
Http://phpqrcode.sourceforge.net/
After downloading the class library provided on the official website, you only need to use phpqrcode. php to generate a QR code. of course, you must enable GD2 in your PHP environment.
Phpqrcode. php provides a key png () method.
The $ text parameter indicates generating two pieces of information text;
The parameter $ outfile indicates whether to output the QR code image file. the default value is No;
The $ level parameter indicates the coverage rate, that is, the covered areas can also be identified, namely, L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25% ), H (QR_ECLEVEL_H, 30% );
The parameter $ size indicates the size of the generated image. the default value is 3. the parameter $ margin indicates the gap between blank areas in the border around the QR code;
The parameter $ saveandprint indicates 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 = QRencode: factory ($ level, $ size, $ margin );
Return $ enc-> encodePNG ($ text, $ outfile, $ saveandprint = false );
}
Calling php qr Code is very simple, the following Code can generate a content for "http://www.cnblogs.com/txw1958/" QR Code.
Include 'phpqrcode. php ';
QRcode: png ('http: // www.cnblogs.com/txw1958 /');

In practice, we will add our own LOGO in the middle of the QR code to enhance the promotion effect. How to generate a QR code containing a logo? In fact, the principle is very simple. First, use the php qr Code to generate a QR Code image, and then use the php image function to add the prepared logo image to the middle of the generated original QR Code image, then generate a new QR code image.
The code is as follows:
Include 'phpqrcode. php ';
$ Value = 'http: // www.cnblogs.com/txw1958/'; // QR code content
$ ErrorCorrectionLevel = 'l'; // fault tolerance level
$ MatrixPointSize = 6; // generate the image size
// Generate a QR code image
QRcode: png ($ value, 'qrcode.png ', $ errorCorrectionLevel, $ matrixPointSize, 2 );
$ Logo = 'logo.png '; // prepared logo image
$ QR = 'qrcode.png '; // generated original QR code
If ($ logo! = FALSE ){
$ QR = imagecreatefromstring (file_get_contents ($ QR ));
$ Logo = imagecreatefromstring (file_get_contents ($ logo ));
$ QR_width = imagesx ($ QR); // the image width of the QR code.
$ QR_height = imagesy ($ QR); // The Image height of the QR code.
$ 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;
// Re-combine and resize the image
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 '';
Include 'phpqrcode. php ';
$ Value = 'http: // www.cnblogs.com/txw1958/'; // QR code content
$ ErrorCorrectionLevel = 'l'; // fault tolerance level
$ MatrixPointSize = 6; // generate the image size
// Generate a QR code image
QRcode: png ($ value, 'qrcode.png ', $ errorCorrectionLevel, $ matrixPointSize, 2 );
$ Logo = 'logo.png '; // prepared logo image
$ QR = 'qrcode.png '; // generated original QR code
If ($ logo! = FALSE ){
$ QR = imagecreatefromstring (file_get_contents ($ QR ));
$ Logo = imagecreatefromstring (file_get_contents ($ logo ));
$ QR_width = imagesx ($ QR); // the image width of the QR code.
$ QR_height = imagesy ($ QR); // The Image height of the QR code.
$ 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;
// Re-combine and resize the image
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 the QR code allows some fault tolerance, even if the hidden part of the QR code is still decoded, the scan results can be decoded when we scan the QR code in less than half, this is because the generator repeatedly expresses some information to improve the fault tolerance. This is why we add a LOGO image in the middle of the QR code without affecting the decoding result.

The above introduces hp qr Code is a php qr Code generation class library, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.