HP QR code is a php two-dimensional code generation class library, it can easily generate two-dimensional code, the official website provides a download and a number of demo demos, to see the address:
http://phpqrcode.sourceforge.net/
After downloading the class library provided by the official website, we only need to use phpqrcode.php to generate the QR code, of course, your PHP environment must turn on support GD2.
Phpqrcode.php provides a key PNG () method, where
The parameter $text represents the generated two-bit information text;
The parameter $outfile indicates whether the output QR code picture file, default no;
The parameter $level represents the fault tolerance, i.e., the area covered is also recognized, respectively 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 generated picture size, the default is 3, and the parameter $margin indicates the spacing value of the border space around the two-dimensional 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) { c1/> $enc = Qrencode::factory ($level, $size, $margin); Return $enc->encodepng ($text, $outfile, $saveandprint =false); }
Calling PHP QR code is very simple, and the following code generates a QR code that reads "http://www.cnblogs.com/txw1958/".
Include ' phpqrcode.php '; QRCode::p ng (' http://www.cnblogs.com/txw1958/');
In the actual application, we will add their own logo in the middle of the two-dimensional code, has enhanced the publicity effect. How do you generate a QR code that contains a logo? In fact, the principle is very simple, first use PHP QR code to generate a two-dimensional code image, and then the use of PHP image correlation function, the pre-prepared logo image is added to the newly generated image of the original two-dimensional code, and then regenerate 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 picture size//generate QR code picture qrcode::p ng ($value, ' qrcode.png ', $errorCorrectionLevel, $matrixPointSize, 2); $logo = ' logo.png ';///Ready logo image $QR = ' qrcode.png ';//The original two-dimensional code figure already generated if ($logo!== FALSE) {$QR = Imagecreatefromstr ING (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 Picture width $logo _height = Imagesy ($logo),//logo picture 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; Regroup the picture and resize imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $logo _width, $logo _height); }//Output picture 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 picture size//generate QR code picture qrcode::p ng ($value, ' qrcode.png ', $errorCorrectionLevel, $matrixPointSize, 2); $logo = ' logo.png ';///Ready logo image $QR = ' qrcode.png ';//The original two-dimensional code figure already generated if ($logo!== FALSE) {$QR = Imagecreatefromstr ING (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 Picture width $logo _height = Imagesy ($logo),//logo picture 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; Regroup the picture and resize imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $logo _width, $logo _height); }//Output picture imagepng ($QR, 'Helloweixin.png '); Echo ' ';
Because the two-dimensional code allows a certain degree of fault tolerance, the general QR code even in the masked part but still able to decode, often we scan the QR code when scanned to even less than half of the scanning results can be decoded, because the generator will be part of the information repeated representation to improve its fault tolerance, This is why we add a logo image in the middle of the QR code does not affect the decoding result of the reason.
HP QR Code (PHP Two-dimensional code generation class library)