Create an account


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

#1
[Tut] PHP QR Code Generator with phpqrcode Library

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2023/03/php-qr-code-generator-with-phpqrcode-library.jpg" width="550" height="367" title="" alt="" /></div><div><div class="modified-on" readability="7.0909090909091"> by <a href="https://phppot.com/about/">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 :)');
</code></pre>
<h3>Installing PHP QR code library</h3>
<p><a href="https://sourceforge.net/projects/phpqrcode/files/">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="https://phppot.com/php/how-to-generate-qr-code-in-php/">PHP QR code generator</a> using the tc-lib-barcode library.</p>
<p><img decoding="async" loading="lazy" class="alignnone size-large wp-image-20682" src="https://phppot.com/wp-content/uploads/2023/03/qr-code-data-550x367.jpg" alt="qr code data" width="550" height="367" srcset="https://phppot.com/wp-content/uploads/2023/03/qr-code-data-550x367.jpg 550w, https://phppot.com/wp-content/uploads/20...00x200.jpg 300w, https://phppot.com/wp-content/uploads/20...68x512.jpg 768w, https://phppot.com/wp-content/uploads/20...e-data.jpg 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 :)');
&lt;?php
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 :)');
?&gt;
</code></pre>
<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;
</code></pre>
<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;
?&gt;
&lt;img src="generate.php?id=&lt;?php echo $QRParameter; ?&gt;" /&gt;
</code></pre>
<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);
}
?&gt;
</code></pre>
<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);
}
?&gt;
&lt;img src="&lt;?php echo $filePath; ?&gt;" /&gt;
</code></pre>
<h2>4. Configure QR code ECC Level, Zoom factor, and Frame size</h2>
<p>The PHP QRCode Library <a href="https://phppot.com/php/php-constants/">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>
<ul>
<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>
</ul>
<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;
</code></pre>
<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;
</code></pre>
<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
?&gt;
&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;
</code></pre>
<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);
?&gt;
&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);
?&gt;
&lt;img src="&lt;?php echo $target; ?&gt;phone-to-text.png" /&gt;
</code></pre>
<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);
?&gt;
&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="https://phppot.com/php/how-to-export-contact-as-vcard-using-php/">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);
?&gt;
&lt;img src="&lt;?php echo $target; ?&gt;vcard-qr-code.png" /&gt; </code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/php-qr-code.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2023/03/...e-library/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016