PHP uses the phpqrcode class to generate a QR code.
This example describes how PHP generates two-dimensional codes based on the phpqrcode class. We will share this with you for your reference. The details are as follows:
It is still difficult to generate a QR code using the PHP language. Of course, except for calling the interface for generating a QR code image (for example, the interface of the graph network http://www.liantu.com/). If you write your own code to generate a QR code, you cannot start. However, we can use phpqrcode, a ready-made class file, to generate a class library using the php qr code, which can easily generate a QR code.
Preparations:
1. phpqrcode class file download,: https://sourceforge.net/projects/phpqrcode/
2. Support for the GD2 extension library must be enabled in the PHP environment (normally, it is enabled)
Explanation:
The downloaded class file is a compressed package containing many files and demo programs. We only need the phpqrcode. php file to generate a QR code. It is a collection file of multiple classes. We need to use the QRcode class (2,963rd rows) png () method (3,090th rows ):
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);}
1st parameters $ text: the content contained in the QR code, including links, text, and json strings;
2nd parameters $ outfile: The default value is false. If no file is generated, only the QR code image is returned and output; otherwise, the file name and path of the generated QR code image must be provided;
$ Level: the default value is L. The values passed by this parameter are L (QR_ECLEVEL_L, 3rd), M (QR_ECLEVEL_M, 7%), and Q (QR_ECLEVEL_Q, 15%), H (QR_ECLEVEL_H, 30%), this parameter controls the two-dimensional code renewal rate. Different parameters indicate the percentage of areas that can be covered by the two-dimensional code, that is, the covered areas can also be identified;
4th parameter $ size: controls the size of the generated image. The default value is 4;
5th parameters $ margin: controls the size of the blank area of the generated QR code;
6th parameters $ saveandprint: Save and display the QR code image. $ outfile must pass the image path;
Example:
1. Generate a QR code (generate an image file)
// 1. function scerweima ($ url = '') {require_once 'phpqrcode. php '; $ value = $ url; // QR code content $ errorCorrectionLevel = 'l'; // fault tolerance level $ matrixPointSize = 5; // generate image size // generate a QR code image $ filename = 'qrcode/'.microtime().'.png '; qrcode: png ($ value, $ filename, $ errorCorrectionLevel, $ matrixPointSize, 2 ); $ QR = $ filename; // generated original QR code Image File $ QR = imagecreatefromstring (file_get_contents ($ QR); // output image imagepng ($ QR, 'qrcode.png '); imagedestroy ($ QR); return '';} // call the echo scerweima ('https: // www.baidu.com ');
2. Add the logo (generating image files) to the generated QR code)
// 2. add the logo (generating image file) function scerweima1 ($ url = '') {require_once 'phpqrcode. php '; $ value = $ url; // QR code content $ errorCorrectionLevel = 'H'; // fault tolerance level $ matrixPointSize = 6; // generate image size // generate a QR code image $ filename = 'qrcode/'.microtime().'.png '; qrcode: png ($ value, $ filename, $ errorCorrectionLevel, $ matrixPointSize, 2 ); $ logo = 'qrcode/logo.jpg '; // The prepared logo image $ QR = $ filename; // The generated original QR code graph if (file_exists ($ logo )){ $ QR = imagecreatefromstring (file_get_contents ($ QR); // connect resources to the target image. $ Logo = imagecreatefromstring (file_get_contents ($ logo); // source image connection resource. $ 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/4; // The width of the logo after combination (1/5 of the QR code) $ scale = $ logo_width/$ logo_qr_width; // The logo width scaling ratio (its own width/the width after combination) $ logo_qr_height = $ logo_height/$ scale; // height of the logo after combination $ from_width = ($ QR_width-$ logo_qr_width)/2; // coordinate point in the upper left corner of the logo after combination // re-combine the image and adjust the size./** imagecopyresampled () combines an image (source image) copy A Square area in */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 '';} // call the echo scerweima1 ('https: // www.baidu.com ') to view the result ');
3. Generate QR code (do not generate image files)
// 3. function scerweima2 ($ url = '') {require_once 'phpqrcode. php '; $ value = $ url; // QR code content $ errorCorrectionLevel = 'l'; // fault tolerance level $ matrixPointSize = 5; // generate image size // generate a QR code image $ QR = QRcode: png ($ value, false, $ errorCorrectionLevel, $ matrixPointSize, 2 );} // call to view the result scerweima2 ('https: // www.baidu.com ');
The first two methods generate a local QR code image each time they are called. The third method directly outputs the QR code to the browser without generating a file.
PS: here we recommend two online QR code tools for your reference:
Online QR code generation tool (enhanced Edition)
Http://tools.jb51.net/transcoding/jb51qrcode
Online QR code decoding and identification tools
Http://tools.jb51.net/transcoding/trans_qrcode