With the advancement of technology, the application fields of QR codes are becoming more and more extensive. This website has previously introduced the use of 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 QR codes with LOGO images in the middle.
Generate QR code using Google API
Google provides a relatively complete QR code generation interface. It is very simple to call the API interface. The following is the calling code:
$urlToEncode="https://";
generateQRfromGoogle($urlToEncode);
/**
* Google API QR code generation [QRcode can store any text of up to 4296 alphanumeric types. For details, you can view the QR code data format]
* @param string $chl The information contained in the QR code can be numbers, characters, binary information, and Chinese characters.
The data types cannot be mixed, the data must pass UTF-8 URL-encoded
* @param int $widhtHeight The size setting of the QR code generated
* @param string $EC_level Optional error correction level, QR code supports four levels of error correction, used to recover lost, wrongly read, fuzzy, and data.
*
*
Q-Can identify data that has been lost by 25%
*
* @param int $margin The distance from the image border generated QR code
*/
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
$chl = urlencode($chl);
echo '<img src="/chart?chs='.$widhtHeight.'x'.$widhtHeight.'
&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.'
" widhtHeight="'.$widhtHeight.'"/>';
}
Use PHP QR code to generate the class library PHP QR Code to generate the QR code
PHP QR Code is a PHP QR code generation library. It can easily generate QR codes. The official website provides downloads and multiple demos, viewing address: /.
After downloading the class library provided by the official website, you only need to use it to generate a QR code. Of course, your PHP environment must be enabled to support GD2. A key png() method is provided, where the parameter $text represents the generated information text of two-digits; the parameter $outfile represents whether to output the QR code image file, default is no; the parameter $level represents the fault tolerance, that is, there are covered areas that can be recognized, namely, L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%); the parameter $size represents the generated image size, default is 3; the parameter $margin represents the spacing value of the blank area around the border of the QR code; the parameter $saveandprint represents 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 QR code with the content "https://".
include '';
QRcode::png('https://');
In actual application, we will add our own logo in the middle of the QR code, which has enhanced the publicity effect. So how to generate a QR code with a logo? In fact, the principle is very simple. First use PHP QR Code to generate a QR code picture, then use the PHP image-related function to add the pre-prepared logo picture to the middle of the newly generated original QR code picture, and then regenerate a new QR code picture.
include '';
$value = 'https://'; //QR code content
$errorCorrectionLevel = 'L';//Fault tolerance level
$matrixPointSize = 6;// Generate image size
//Generate QR code pictures
QRcode::png($value, '', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = '';//Prepainted logo image
$QR = '';//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);//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;
//Recombine the picture and resize it
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//Output picture
imagepng($QR, '');
echo '<img src="">';
Since QR codes allow certain fault tolerance, general QR codes can still be decoded even if they cover the part. Often, when we scan the QR code, we can decode the scan results when they scan less than half of them. This is because the generator will repeatedly represent some information to improve its fault tolerance. This is why we add a LOGO picture in the middle of the QR code does not affect the decoding result.