Two methods and examples of generating QR codes with PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:26:28
Original
725 people have browsed it

With the advancement of technology, the application fields of QR codes are becoming more and more extensive. There are previous articles on this site introducing the use of jQuery plug-ins 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 LOGO image in the middle.

Use Google API to generate QR code

Google provides a relatively complete QR code generation interface. It is very simple to call the API interface. The following is the calling code:

Copy code The code is as follows:

$urlToEncode="http://www.jb51.net";
generateQRfromGoogle($urlToEncode);
/**
* google api QR code generation [QRcode can store up to 4296 alphanumeric characters of any text, please check the QR code data format for details]
* @param string $chl The information contained in the QR code can be numbers, characters, binary information, or Chinese characters.
Data types cannot be mixed, data must be UTF-8 URL-encoded
* @param int $widhtHeight Size setting for generating QR code
* @param string $EC_level Optional error correction level. QR code supports four levels of error correction, which is used to recover lost, misread, ambiguous, and data.
* L-Default: Can identify 7% of lost data
* M-can identify data that has lost 15%
* Q-Can identify data that has lost 25%
* H-Can identify data that has lost 30%
* @param int $margin The distance between the generated QR code and the image border
​*/
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
$chl = urlencode($chl);
echo 'QR code';
}

Use PHP QR code generation library PHP QR Code to generate QR code

PHP QR Code is a PHP QR code generation library that can be used to easily generate QR codes. The official website provides downloads and multiple demonstration demos. View address: http://phpqrcode.sourceforge.net/.
After downloading the class library provided by the official website, 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; the parameter $outfile indicates whether to output a QR code image file, the default is no; the parameter $level indicates the fault tolerance rate, that is The covered areas can still be identified, which are 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 is 3; the parameter $margin indicates the spacing value of the blank area of ​​the border around the QR code; the parameter $saveandprint indicates whether to save the QR code and display it.

Copy code 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 QR code with the content "http://www.jb51.net".
Copy code The code is as follows:

include 'phpqrcode.php';
QRcode::png('http://www.jb51.net');

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 newly generated original QR code image, and then regenerate a new one. QR code picture.
Copy code The code is as follows:

include 'phpqrcode.php';
$value = 'http://www.jb51.net'; //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';//Prepared logo image
$QR = 'qrcode.png';//The original QR code image that has been generated

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;
//Regroup and resize images
Imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//Output picture
imagepng($QR, 'helloweba.png');
echo '';

Since 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 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824698.htmlTechArticleWith the advancement of technology, the application fields of QR codes are becoming more and more extensive. This site has previously introduced articles. Use the jQuery plug-in to generate QR codes. Today I will share with you how to use PHP to generate...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!