of with logo image Two ways to generate a QR code using PHP

  • 2021-01-25 07:20:36
  • OfStack

1. Generate 2-dimensional code using Google API
Google provides a relatively complete 2-D code generation interface. Calling the API interface is very simple. Here is the code:


$urlToEncode="https://www.ofstack.com"; 
generateQRfromGoogle($urlToEncode); 
/** 
 * google api 2 Dimension code generation QRcode Can store up to 4296 An alphanumeric type of any text, specific can be seen 2 Dimension Code Data Format  
 * @param string $chl 2 Dimensional codes contain information, which can be numbers, characters, 2 Base information, Chinese characters.  
  Cannot mix data types, data must pass through UTF-8 URL-encoded 
 * @param int $widhtHeight  generate 2 Dimension setting of dimension code  
 * @param string $EC_level  Optional error correction level, QR Code to support 4 Three levels of error correction, used to recover lost, misread, fuzzy, data.  
 * L- Default: can identify the lost 7% The data of  
 * M- Losses can be identified 15% The data of  
 * Q- Losses can be identified 25% The data of  
 * H- Losses can be identified 30% The data of  
 * @param int $margin  The generated 2 Dimensional code from the image border distance  
 */ 
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') 
{ 
 $chl = urlencode($chl); 
 echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.' 
 &cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.' 
 " widhtHeight="'.$widhtHeight.'"/>'; 
} 


2. Use ES11en2-dimensional code generation library PHP QR Code to generate 2-dimensional code

PHP QR Code is PHP2 1 d code generated class libraries, it can easily generate 2 d code, website provides downloads and multiple demo demonstration, check the address: http: / / phpqrcode sourceforge. net /.
After downloading the class library provided by the official website, you only need to use phpqrcode.php to generate 2-D code. Of course, your PHP environment must be enabled to support GD2. phpqrcode. php provides a key png() method, where the $text parameter means to generate 2-bit information text; Parameter $outfile indicates whether to output 2-dimensional code image file, default no; The parameter $level indicates the fault tolerance rate, that is, the covered area can also be identified, respectively L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%); The $size parameter indicates the size of the generated image. The default value is 3. The parameter $margin represents the spacing of the border blank area around the 2-dimensional code; The parameter $saveandprint indicates whether to save the 2-D 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); 
} 

Call PHP QR Code is very simple and can be produced to the following code 1 piece of content is "https: / / www. ofstack. com" 2 d code.
Php code
include 'phpqrcode.php';
QRcode::png('https://www.ofstack.com');

Then in the practical application, we will add our own LOGO in the middle of the 2-D code, which has enhanced the publicity effect. So how to generate a 2-D code with logo? In fact, the principle is very simple, first use PHP QR Code to generate a 2-D code picture, and then use image related functions of php to add the prepared logo picture to the original 2-D code picture that has just been generated, and then regenerate a new 2-D code picture.


include 'phpqrcode.php'; 
$value = 'https://www.ofstack.com'; //2 D code content  
$errorCorrectionLevel = 'L';// Fault tolerance level  
$matrixPointSize = 6;// Generate image size  
// generate 2 D code pictures  
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'logo.png';// The prepared logo The picture  
$QR = 'qrcode.png';// The original that has been generated 2 D code  
 
if ($logo !== FALSE) { 
 $QR = imagecreatefromstring(file_get_contents($QR)); 
 $logo = imagecreatefromstring(file_get_contents($logo)); 
 $QR_width = imagesx($QR);//2 Dimensional code image width  
 $QR_height = imagesy($QR);//2 Dimensional code image height  
 $logo_width = imagesx($logo);//logo Image 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 images and resize them  
 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
 $logo_qr_height, $logo_width, $logo_height); 
} 
// The output image  
imagepng($QR, 'helloweba.png'); 
echo '<img src="helloweba.png">'; 


Please save the following code as img.php if you don't need to produce image files


<?php
include 'phpqrcode.php'; 
$value = $_GET['url'];//2 D code content  
$errorCorrectionLevel = 'L';// Fault tolerance level  
$matrixPointSize = 6;// Generate image size  
// generate 2 D code pictures  
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'jb51.png';// The prepared logo The picture  
$QR = 'qrcode.png';// The original that has been generated 2 D code  

if ($logo !== FALSE) { 
 $QR = imagecreatefromstring(file_get_contents($QR)); 
 $logo = imagecreatefromstring(file_get_contents($logo)); 
 $QR_width = imagesx($QR);//2 Dimensional code image width  
 $QR_height = imagesy($QR);//2 Dimensional code image height  
 $logo_width = imagesx($logo);//logo Image 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 images and resize them  
 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
 $logo_qr_height, $logo_width, $logo_height); 
} 
// The output image  
Header("Content-type: image/png");
ImagePng($QR);

Call the method:

< img src=http://test.ofstack.com:8080/qr/img.php?url=https://www.ofstack.com/1.rar >



Since 2 d code allows one fault tolerance, even in obscure part 1 of 2 d yards but still be able to decode, often when we scan the 2 d code scanning to even less than 1 and a half can decode the scan results, this is because some information for generator will repeat said to improve the degree of fault tolerance, that's why we're in the middle of the 2 d code add a LOGO images does not affect the decoding result.

PS: This site also provides a 10-point powerful 2-D code tool for you to use:

http://tools.ofstack.com/transcoding/jb51qrcode


Related articles: