Use PHP to generate a summary of QR codes, and use php to summarize
With the advancement of technology, the application of QR codes has become more and more extensive. previous articles on this site have introduced how to use the jQuery plug-in to generate QR codes. Today I will share with you how to use PHP to generate QR codes, and how to generate a QR code with a central LOGO image.
Use Google API to generate QR code
Google provides a comprehensive interface for generating QR codes. The following code calls an API:
$ UrlToEncode = "http://www.bkjia.com"; generateQRfromGoogle ($ urlToEncode);/*** google api QR code generation [QRcode can store up to 4296 letters and numbers of any text, for details, see the QR code data format. * @ param string $ information contained in the two-dimensional code, which can be numbers, characters, binary information, and Chinese characters. Cannot mix data type, data must go through the UTF-8 URL-encoded * @ param int $ widhtHeight to generate the dimensional settings of the QR code * @ param string $ EC_level optional Error Correction level, QR code supports four levels of error correction, used to restore lost, read-wrong, fuzzy, and data. * L-default: 7% of lost data can be identified * M-15% of lost data can be identified * Q-25% of lost data can be identified * H-30% of lost data can be identified *@ param int $ the distance between the QR code generated by margin and the image border */function generateQRfromGoogle ($ chlorophyll, $ widhtHeight = '000000', $ EC_level = 'l', $ margin = '0') {$ chlorophyll = urlencode ($ chlorophyll); echo ' ';}
Use php qr Code to generate a class library php qr Code to generate a QR Code
Php 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 at: 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 parameter $ text indicates that two pieces of information are generated. The parameter $ outfile indicates whether to output the two-dimensional 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%); parameter $ size indicates the size of the generated image; default value: 3; parameter $ margin indicates the gap between blank areas of the border around the QR code; the parameter $ saveandprint indicates whether to save the QR code and display 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); }
Calling php qr Code is very simple, the following Code can generate a content for "http://www.bkjia.com" QR Code.
include 'phpqrcode.php'; QRcode::png('http://www.bkjia.com');
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.
Include 'phpqrcode. php '; $ value = 'HTTP: // www.bkjia.com'; // QR code content $ errorCorrectionLevel = 'l'; // fault tolerance level $ matrixPointSize = 6; // generate image size // generate QR code image QRcode: png ($ value, 'qrcode.png ', $ errorCorrectionLevel, $ matrixPointSize, 2); $ logo = 'logo.png '; // The prepared logo image $ QR = 'qrcode.png '; // The generated original QR code image if ($ logo! = FALSE) {$ QR = imagecreatefromstring (file_get_contents ($ QR); $ logo = imagecreatefromstring (file_get_contents ($ logo); $ QR_width = imagesx ($ QR ); // QR code Image Width $ 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_width; $ logo_qr_height = $ logo_height/$ scale; $ from_width = ($ QR_width-$ logo_qr_width)/2; // re-combine the image and resize imagecopyresampled ($ QR, $ logo, $ from_width, $ from_width, 0, 0, $ logo_qr_width, $ logo_qr_height, $ logo_width, $ logo_height);} // output image imagepng ($ QR, 'jb51.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 is all the content of this article. I hope it will be helpful for you to master the php production QR code.