Create an account

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] PHP QR Code Generator with phpqrcode Library

PHP QR Code Generator with phpqrcode Library

<div style="margin: 5px 5% 10px 5%;"><img src="" width="550" height="367" title="" alt="" /></div><div><div class="modified-on" readability="7.0909090909091"> by <a href="">Vincy</a>. Last modified on March 28th, 2023.</div>
<p>QR code (Quick Response code) is a machine-readable pictorial format containing black and white squares. It is used for storing information like URLs, product ID, etc. It is a type of matrix barcode or a two-dimensional barcode.</p>
<p>It is a convenient form of storing and retrieving simple data and has become even more popular with the advent of smartphones. The camera in the smartphone can act as a reader and read the QR code and help to decipher the data stored in it.</p>
<p>This article gives many examples if you want a solution to generate QR codes in PHP. There are a lot of PHP libraries available to generate QR codes. This article uses the PHP QR code library.</p>
<h2>1. Quick example</h2>
<p>This quick example returns the QR code to the browser in one line. It produces the output as a PNG stream.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php'; // Displays QR Code image to the browser
QRcode::png('PHP QR Code Smile');
<h3>Installing PHP QR code library</h3>
<p><a href="">Download the latest library</a> version and extract it into your application vendor folder. The source code downloadable in this article has the library.</p>
<p>All the examples in this article have the code to include the suitable library file to use its feature.</p>
<p>In a previous article, we have seen code for a <a href="">PHP QR code generator</a> using the tc-lib-barcode library.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-20682" src="" alt="qr code data" width="550" height="367" srcset=" 550w, 300w, 768w, 1200w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>2. Display the QR code using an HTML image</h2>
<p>To display the QR code using an HTML image tag, link the HTML image source to the PHP file that returns the QR code PNG data.</p>
<p>The generate.php file returns the QR code in the below code using the PHP QrCode library. The HTML image refers to this file to show the QR code in the browser.</p>
<p class="code-heading">show-qr-code-in-HTML-img/generate.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php'; // Displays QR Code image to the browser
QRcode::png('PHP QR Code Smile');
require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php'; // outputs image as PNG that can be refered to a HTML image 'src'
QRcode::png('PHP QR Code Smile');
<p class="code-heading">show-qr-code-in-HTML-img/view.php</p>
<pre class="prettyprint"><code class="language-html">&lt;img src="generate.php" /&gt;
<h2>2. Passing arguments to the QR code generator</h2>
<p>Passing parameters to a QR code generator helps to perform dynamic processing about the parameter.</p>
<p>For example, we can pass a contact id to retrieve contact information to bundle it with the QR code output.</p>
<p>This example shows how to send parameters and process them in the QR generator.</p>
<p>The view.php file prepares the QR code parameter in PHP. Then, it sends it to the generate.php in query. This URL with the QR code parameter is specified to an HTML image source.</p>
<p class="code-heading">show-qr-code-in-html-img/view.php</p>
<pre class="prettyprint"><code class="language-php-template">&lt;?php
// initialize PHP parameter to send to the QR code generator
$QRParameter = 1234;
&lt;img src="generate.php?id=&lt;?php echo $QRParameter; ?&gt;" /&gt;
<p>In this generate.php file, it does simple manipulation to the passed argument. It prefixes a string with the GET parameter received and bundles the manipulated line to the QR code PNG.</p>
<p class="code-heading">show-qr-code-in-html-img/generate.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
if (empty($_GET['id'])) { echo "&lt;b&gt;ERROR:&lt;/b&gt; Bad request. Required parameters are missing."; exit;
} else { require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php'; $inputString = $_GET['id']; // Do not return anything to the browser ob_start("callback"); // Process the input string $codeText = 'DEMO - ' . $inputString; // end of processing $debugLog = ob_get_contents(); ob_end_clean(); // outputs QR code as a PNG data QRcode::png($codeText);
<h2>3. Save the QR code on the server</h2>
<p>This example saves the generated QR code on the server.</p>
<p>It defines the QR code data and the png file name suffix in an array. Then, it uses them while creating the target to save the QR code.</p>
<p class="code-heading">save-qr-code-in-server.php</p>
<pre class="prettyprint"><code class="language-php-template">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php'; // how to configure silent zone (frame) size
$qrContent = array('content' =&gt; 'Contact information', 'slug' =&gt; 'contact-info'); $target = "uploads/qr-code-dir/";
$filePath = $target . 'ouput-qr-code-' . $qrContent['slug'] . '.png'; if (!file_exists($filePath)) { QRcode::png($qrContent['content'], $filePath);
&lt;img src="&lt;?php echo $filePath; ?&gt;" /&gt;
<h2>4. Configure QR code ECC Level, Zoom factor, and Frame size</h2>
<p>The PHP QRCode Library <a href="">defines constants</a> for different ECC levels, Zoom factor, and Frame size. These factors are used for the following purposes when generating a QR code.</p>
<p>This program creates QR codes in L, M, Q, and H levels with appropriate QR constants.</p>
<li>ECC level is the allowed percentage of damage without affecting reading data.</li>
<li>Zoom factor is the allowed resolution that can be changed based on the use cases.</li>
<li>Silent zone Frame size – The silent or quiet zone frame size varies based on different matrices. Setting the QR code above or equal to 4 blocks is recommended.</li>
<p class="code-heading">qr-code-ecc-level.php</p>
<pre class="prettyprint"><code class="language-php-template">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
require_once __DIR__ . '/vendor/phpqrcode/qrconst.php'; $qrContent = 'Demo data to bundle into a QR code'; $target = "uploads/ecc-level/"; // generating QR code in the 4 ECC level
QRcode::png($qrContent, $target . 'l.png', QR_ECLEVEL_L);
QRcode::png($qrContent, $target . 'm.png', QR_ECLEVEL_M);
QRcode::png($qrContent, $target . 'q.png', QR_ECLEVEL_Q);
QRcode::png($qrContent, $target . 'h.png', QR_ECLEVEL_H); ?&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;m.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;q.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;h.png" /&gt;
<p>This program adds the zoom factor 1 to 4 with the QR_ECLEVEL_L ECC constant.</p>
<p class="code-heading">qr-code-pixel-zoom-factor.php</p>
<pre class="prettyprint"><code class="language-php-template">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php'; $qrContent = 'Demo data to bundle into a QR code with zoom factor'; $target = "uploads/pixel-zoom-qr-code/"; // generating QR code with 4 ECC level and zoom factor
QRcode::png($qrContent, $target . 'l_1.png', QR_ECLEVEL_L, 1);
QRcode::png($qrContent, $target . 'l_2.png', QR_ECLEVEL_L, 2);
QRcode::png($qrContent, $target . 'l_3.png', QR_ECLEVEL_L, 3);
QRcode::png($qrContent, $target . 'l_4.png', QR_ECLEVEL_L, 4); ?&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_1.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_2.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_3.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_4.png" /&gt;
<p>This program creates QR codes with all these factors ECC, Zoom, and Frame constants of the library.</p>
<p class="code-heading">silent-zone-frame-size.php</p>
<pre class="prettyprint"><code class="language-php-template">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php'; $qrContent = 'Demo data to bundle into a QR code with frame size'; $target = "uploads/pixel-zoom-qr-code/"; // generating // frame config values below 4 are not recommended !!!
QRcode::png($qrContent, $target . 'l_3_4.png', QR_ECLEVEL_L, 3, 4);
QRcode::png($qrContent, $target . 'l_3_6.png', QR_ECLEVEL_L, 3, 6);
QRcode::png($qrContent, $target . 'l_3_10.png', QR_ECLEVEL_L, 3, 10); // displaying
&lt;img src="&lt;?php echo $target; ?&gt;l_3_4.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_3_6.png" /&gt;
&lt;img src="&lt;?php echo $target; ?&gt;l_3_10.png" /&gt;
<h2>5. Add phone, email, and contact info to a QR code</h2>
<p>This section has PHP examples to create QR codes to attach contact information.&nbsp; It prepares the QR code content with the “tel:”. “sms:” and “mail:” links and attaches it to the QR code.</p>
<p class="code-heading">add-phone-to-call.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$phone = '(091)700-001-710';
// attache phone to call
$qrContent = 'tel:' . $phone;
QRcode::png($qrContent, $target . 'phone-to-call.png', QR_ECLEVEL_L, 3);
&lt;img src="&lt;?php echo $target; ?&gt;phone-to-call.png" /&gt; </code></pre>
<p class="code-heading">add-phone-to-text.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$phone = '(091)700-001-710';
// Attach the phone to text
$qrContent = 'sms:' . $phone;
// generating
QRcode::png($qrContent, $target. 'phone-to-text.png', QR_ECLEVEL_L, 3);
&lt;img src="&lt;?php echo $target; ?&gt;phone-to-text.png" /&gt;
<p>Attaching an email adds the mail subject and body as part of the mail recipient data. But the subject and the body parameters are optional.</p>
<p class="code-heading">add-recipient-to-send-mail.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$recipient = '[email protected]';
$mailSubject = 'Enquiry';
$mailBody = 'Post enquiry content';
// Prepare QR content with email recipient, subject and body
$qrContent = 'mailto:' . $recipient . '?subject=' . urlencode($mailSubject) . '&amp;body=' . urlencode($mailBody);
// Attach maileto link to the QRCode
QRcode::png($qrContent, $target. 'mail.png', QR_ECLEVEL_L, 3);
&lt;img src="&lt;?php echo $target; ?&gt;mail.png" /&gt; </code></pre>
<p>This QR code example creates a v-card to bundle with the QR code. It includes basic details with the v-card.</p>
<p>This library allows adding more details to the QR code. For example, it can attach a contact avatar to a QR code.</p>
<p>In a previous article, we have seen <a href="">how to generate and download a v-card</a> for contact information.</p>
<p class="code-heading">save-vcard-to-qr-code.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
// Contact details
$name = 'Example1';
$phone = '(091)700-001-711';
// QR code content with VACARD
$qrCode = 'BEGIN:VCARD' . "\n";
$qrCode .= 'FN:' . $name . "\n";
$qrCode .= 'TEL;WORK;VOICE:' . $phone . "\n";
$qrCode .= 'END:VCARD';
// Attaching VCARD to QR code
QRcode::png($qrCode, $target. 'vcard-qr-code.png', QR_ECLEVEL_L, 3);
&lt;img src="&lt;?php echo $target; ?&gt;vcard-qr-code.png" /&gt; </code></pre>
<p><a class="download" href="">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
<p> <a href="" class="top">↑ Back to Top</a> </p>

Forum Jump:

Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016