Tuesday, June 25, 2024
HomePHPPHP QR Code Generator with phpqrcode Library

PHP QR Code Generator with phpqrcode Library


by Vincy. Final modified on March thirtieth, 2023.

QR code (Fast Response code) is a machine-readable pictorial format containing black and white squares. It’s used for storing data like URLs, product ID, and so forth. It’s a kind of matrix barcode or a two-dimensional barcode.

It’s a handy type of storing and retrieving easy information and has turn into much more widespread with the arrival of smartphones. The digicam within the smartphone can act as a reader and skim the QR code and assist to decipher the info saved in it.

This text provides many examples if you’d like an answer to generate QR codes in PHP. There are numerous PHP libraries accessible to generate QR codes. This text makes use of the PHP QR code library.

1. Fast instance

This fast instance returns the QR code to the browser in a single line. It produces the output as a PNG stream.

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// Shows QR Code picture to the browser
QRcode::png('PHP QR Code :)');

Putting in PHP QR code library

Obtain the newest library model and extract it into your software vendor folder. The supply code downloadable on this article has the library.

All of the examples on this article have the code to incorporate the acceptable library file to make use of its characteristic.

In a earlier article, we have now seen code for a PHP QR code generator utilizing the tc-lib-barcode library.

qr code data

2. Show the QR code utilizing an HTML picture

To show the QR code utilizing an HTML picture tag, hyperlink the HTML picture supply to the PHP file that returns the QR code PNG information.

The generate.php file returns the QR code within the under code utilizing the PHP QrCode library. The HTML picture refers to this file to point out the QR code within the browser.

show-qr-code-in-HTML-img/generate.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// Shows QR Code picture to the browser
QRcode::png('PHP QR Code :)');
<?php
require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php';

// outputs picture as PNG that may be refered to a HTML picture 'src'
QRcode::png('PHP QR Code :)');
?>

show-qr-code-in-HTML-img/view.php

<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/generate.php" />

2. Passing arguments to the QR code generator

Passing parameters to a QR code generator helps to carry out dynamic processing concerning the parameter.

For instance, we will move a contact id to retrieve contact data to bundle it with the QR code output.

This instance reveals tips on how to ship parameters and course of them within the QR generator.

The view.php file prepares the QR code parameter in PHP. Then, it sends it to the generate.php in question. This URL with the QR code parameter is specified to an HTML picture supply.

show-qr-code-in-html-img/view.php

<?php
// initialize PHP parameter to ship to the QR code generator
$QRParameter = 1234;
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/generate.php?id=<?php echo $QRParameter; ?>" />

On this generate.php file, it does easy manipulation to the handed argument. It prefixes a string with the GET parameter obtained and bundles the manipulated line to the QR code PNG.

show-qr-code-in-html-img/generate.php

<?php
if (empty($_GET['id'])) {
    echo "<b>ERROR:</b> Dangerous request. Required parameters are lacking.";
    exit;
} else {
    require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php';
    $inputString = $_GET['id'];

    // Don't return something to the browser
    ob_start("callback");
    // Course of the enter string
    $codeText="DEMO - " . $inputString;
    // finish of processing
    $debugLog = ob_get_contents();
    ob_end_clean();
    // outputs QR code as a PNG information
    QRcode::png($codeText);
}
?>

3. Save the QR code on the server

This instance saves the generated QR code on the server.

It defines the QR code information and the png file identify suffix in an array. Then, it makes use of them whereas creating the goal to avoid wasting the QR code.

save-qr-code-in-server.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// tips on how to configure silent zone (body) measurement
$qrContent = array('content material' => 'Contact data', 'slug' => 'contact-info');

$goal = "uploads/qr-code-dir/";
$filePath = $goal . 'ouput-qr-code-' . $qrContent['slug'] . '.png';

if (!file_exists($filePath)) {
    QRcode::png($qrContent['content'], $filePath);
}
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $filePath; ?>" />

4. Configure QR code ECC Degree, Zoom issue, and Body measurement

The PHP QRCode Library defines constants for various ECC ranges, Zoom issue, and Body measurement. These components are used for the next functions when producing a QR code.

This program creates QR codes in L, M, Q, and H ranges with applicable QR constants.

  • ECC degree is the allowed proportion of injury with out affecting studying information.
  • Zoom issue is the allowed decision that may be modified based mostly on the use instances.
  • Silent zone Body measurement – The silent or quiet zone body measurement varies based mostly on completely different matrices. Setting the QR code above or equal to 4 blocks is beneficial.

qr-code-ecc-level.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
require_once __DIR__ . '/vendor/phpqrcode/qrconst.php';

$qrContent="Demo information to bundle right into a QR code";

$goal = "uploads/ecc-level/";

// producing QR code within the 4 ECC degree
QRcode::png($qrContent, $goal . 'l.png', QR_ECLEVEL_L);
QRcode::png($qrContent, $goal . 'm.png', QR_ECLEVEL_M);
QRcode::png($qrContent, $goal . 'q.png', QR_ECLEVEL_Q);
QRcode::png($qrContent, $goal . 'h.png', QR_ECLEVEL_H);

?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>l.png" />
<img src="<?php echo $goal; ?>m.png" />
<img src="<?php echo $goal; ?>q.png" />
<img src="<?php echo $goal; ?>h.png" />

This program provides the zoom issue 1 to 4 with the QR_ECLEVEL_L ECC fixed.

qr-code-pixel-zoom-factor.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

$qrContent="Demo information to bundle right into a QR code with zoom issue";

$goal = "uploads/pixel-zoom-qr-code/";

// producing QR code with 4 ECC degree and zoom issue
QRcode::png($qrContent, $goal . 'l_1.png', QR_ECLEVEL_L, 1);
QRcode::png($qrContent, $goal . 'l_2.png', QR_ECLEVEL_L, 2);
QRcode::png($qrContent, $goal . 'l_3.png', QR_ECLEVEL_L, 3);
QRcode::png($qrContent, $goal . 'l_4.png', QR_ECLEVEL_L, 4);

?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>l_1.png" />
<img src="<?php echo $goal; ?>l_2.png" />
<img src="<?php echo $goal; ?>l_3.png" />
<img src="<?php echo $goal; ?>l_4.png" />

This program creates QR codes with all these components ECC, Zoom, and Body constants of the library.

silent-zone-frame-size.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

$qrContent="Demo information to bundle right into a QR code with body measurement";

$goal = "uploads/pixel-zoom-qr-code/";

// producing

// body config values under 4 will not be beneficial !!!
QRcode::png($qrContent, $goal . 'l_3_4.png', QR_ECLEVEL_L, 3, 4);
QRcode::png($qrContent, $goal . 'l_3_6.png', QR_ECLEVEL_L, 3, 6);
QRcode::png($qrContent, $goal . 'l_3_10.png', QR_ECLEVEL_L, 3, 10);

// displaying
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>l_3_4.png" />
<img src="<?php echo $goal; ?>l_3_6.png" />
<img src="<?php echo $goal; ?>l_3_10.png" />

5. Add cellphone, e mail, and make contact with information to a QR code

This part has PHP examples to create QR codes to connect contact data.  It prepares the QR code content material with the “tel:”. “sms:” and “mail:” hyperlinks and attaches it to the QR code.

add-phone-to-call.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$goal = "uploads/qr-code-phone/";
$cellphone="(091)700-001-710";
// attache cellphone to name
$qrContent="tel:" . $cellphone;
QRcode::png($qrContent, $goal . 'phone-to-call.png', QR_ECLEVEL_L, 3);
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>phone-to-call.png" />   

add-phone-to-text.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$goal = "uploads/qr-code-phone/";
$cellphone="(091)700-001-710";
// Connect the cellphone to textual content
$qrContent="sms:" . $cellphone;
// producing
QRcode::png($qrContent, $goal. 'phone-to-text.png', QR_ECLEVEL_L, 3);
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>phone-to-text.png" />

Attaching an e mail provides the mail topic and physique as a part of the mail recipient information. However the topic and the physique parameters are non-compulsory.

add-recipient-to-send-mail.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$goal = "uploads/qr-code-phone/";
$recipient="vincy@instance.com";
$mailSubject="Enquiry";
$mailBody = 'Submit enquiry content material';
// Put together QR content material with e mail recipient, topic and physique
$qrContent="mailto:" . $recipient . '?topic=" . urlencode($mailSubject) . "&physique=' . urlencode($mailBody);
// Connect maileto hyperlink to the QRCode
QRcode::png($qrContent, $goal. 'mail.png', QR_ECLEVEL_L, 3);
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>mail.png" /> 

This QR code instance creates a v-card to bundle with the QR code. It contains primary particulars with the v-card.

This library permits including extra particulars to the QR code. For instance, it will probably connect a contact avatar to a QR code.

In a earlier article, we have now seen tips on how to generate and obtain a v-card for contact data.

save-vcard-to-qr-code.php

<?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$goal = "uploads/qr-code-phone/";
// Contact particulars
$identify="Example1";
$cellphone="(091)700-001-711";
// QR code content material with VACARD
$qrCode="BEGIN:VCARD" . "n";
$qrCode .= 'FN:' . $identify . "n";
$qrCode .= 'TEL;WORK;VOICE:' . $cellphone . "n";
$qrCode .= 'END:VCARD';
// Attaching VCARD to QR code
QRcode::png($qrCode, $goal. 'vcard-qr-code.png', QR_ECLEVEL_L, 3);
?>
<img src="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/<?php echo $goal; ?>vcard-qr-code.png" /> 

Obtain

↑ Again to High

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments