Two methods to generate QR code using PHP (with logo image)_PHP Tutorial

WBOY
Release: 2016-07-13 10:35:56
Original
1110 people have browsed it

1. 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 types of any text, you can check the QR code data format for details]
* @param string $chl The information contained in the QR code, you can It is numbers, characters, binary information, and Chinese characters.
Data types cannot be mixed, data must be UTF-8 URL-encoded
* @param int $widhtHeight Size settings for generating QR codes
* @param string $EC_level Optional error correction level, QR code Supports four levels of error correction to recover lost, misread, ambiguous, and data.
*                          L-Default: Can identify 7% of the data that has been lost
*                                         Q-Can identify data that has lost 25%
*                                                      -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'; 
} 


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

PHP QR Code is A PHP QR code generation library, which 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 = QRecode::factory($level, $size, $margin);
return $enc- >encodePNG($text, $outfile, $saveandprint=false); www.jb51.net" QR code.
Php code
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';//error 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';//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_wid th = ($QR_width – $logo_qr_width) / 2; , $logo_width, $logo_height);
}
//Output image
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 QR codes, we can’t even scan them. The scan result can be decoded in half the time. 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 result.


http://www.bkjia.com/PHPjc/741844.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/741844.htmlTechArticle1. Generate QR code using Google API Google provides a relatively complete QR code generation interface, calling the API interface Very simple, the following is the calling code: Copy the code as follows: $urlToEnc...
Related labels:
php
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