Two Methods and Examples of Generating Two Dimensional Code by PHP

  • 2021-07-06 10:24:37
  • OfStack

With the progress of science and technology, the application field of 2D code is more and more extensive. This site has previously introduced the use of jQuery plug-in to generate 2D code. Today, I will share with you how to use PHP to generate 2D code and how to generate 2D code with LOGO image in the middle.

Generation of 2-D Codes Using Google API

Google provides a relatively complete 2-dimensional code generation interface, and it is very simple to call API interface. The following is the calling code:


$urlToEncode="https://www.ofstack.com";
generateQRfromGoogle($urlToEncode);
/**
 * google api 2 Dimension code generation " QRcode Can store the most 4296 Any text of alphanumeric type, which can be viewed for details 2 Dimension code data format "
 * @param string $chl 2 Dimension codes contain information, which can be numbers, characters, 2 Binary information, Chinese characters.
  Data types cannot be mixed, and 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 support 4 Three levels of error correction are used to recover lost, misread, ambiguous and data.
 *                            L- Default: You can identify the lost 7% Data of
 *                            M- Loss can be identified 15% Data of
 *                            Q- Loss can be identified 25% Data of
 *                            H- Loss can be identified 30% Data of
 * @param int $margin Generated 2 Distance between dimension code and picture border
 */
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.'"/>';
}

Using PHP 2-D code generation class library PHP QR Code to generate 2-D code

PHP QR Code is an PHP 2-dimensional code generation class library, which can easily generate 2-dimensional codes. Official website provides downloads and multiple demonstrations of demo. The viewing address is http://phpqrcode.sourceforge. net/.
After downloading the class library provided by official website, you only need to use phpqrcode. php to generate 2D code. Of course, your PHP environment must be enabled to support GD2. phpqrcode. php provides a key png () method, in which the parameter $text means generating 2-bit information text; Parameter $outfile indicates whether to output 2-dimensional code picture file, and the default is no; The parameter $level indicates the fault tolerance rate, that is, the covered areas can 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 generated picture size, and the default is 3; The parameter $margin represents the space between the blank areas of the border around the 2-dimensional code; The parameter $saveandprint indicates whether to save the 2D 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 2-dimensional code with the content of "https://www. ofstack. com".

include 'phpqrcode.php';
QRcode::png('https://www.ofstack.com');

Then in practical application, we will add our own LOGO in the middle of 2D code, which has enhanced the publicity effect. How to generate a 2-dimensional code containing logo? In fact, the principle is very simple. First, use PHP QR Code to generate a 2D code picture, and then use image correlation function of php to add the prepared logo picture to the original 2D code picture just generated, and then regenerate a new 2D code picture.
 
include 'phpqrcode.php'; 
$value = 'https://www.ofstack.com'; //2 Dimension code content
$errorCorrectionLevel = 'L';// Fault tolerance level
$matrixPointSize = 6;// Generate picture size
// Generate 2 Dimension code picture
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png';// Ready logo Picture
$QR = 'qrcode.png';// Original that has been generated 2 Dimensional code graph
 
if ($logo !== FALSE) {
    $QR = imagecreatefromstring(file_get_contents($QR));
    $logo = imagecreatefromstring(file_get_contents($logo));
    $QR_width = imagesx($QR);//2 Dimension code picture width
    $QR_height = imagesy($QR);//2 Dimension code picture 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;
    // Reassemble and resize pictures
    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 '<img src="helloweba.png">';

Because 2-D codes allow 1-D fault tolerance, 1-like 2-D code can still be decoded even if it is partially covered. Often, when we scan 2-D code, we can decode the scanning result even less than 1 and a half. This is because the generator will repeatedly represent some information to improve its fault tolerance, which is why adding an LOGO picture in the middle of 2-D code does not affect the decoding result.


Related articles: