Sick Gaming
[Tut] PHP QR Code Generator with chillerlan-php-qrcode Library - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: PHP Development (https://www.sickgaming.net/forum-82.html)
+--- Thread: [Tut] PHP QR Code Generator with chillerlan-php-qrcode Library (/thread-101259.html)



[Tut] PHP QR Code Generator with chillerlan-php-qrcode Library - xSicKxBot - 08-18-2023

[Tut] PHP QR Code Generator with chillerlan-php-qrcode Library

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2023/08/php-qr-code-generator-with-chillerlan-php-qrcode-library.jpg" width="550" height="367" title="" alt="" /></div><div><div class="modified-on" readability="7.0697674418605"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on June 15th, 2023.</div>
<p>This tutorial will create an example for generating a QR code using PHP. This example uses the Chillerlan QR code library. It is a PHP library with advanced features for creating QR codes, bar codes, and more.</p>
<p>There are two examples in this project. The first is a basic use-case scenario, and the second is an advanced example.</p>
<p>Both will help familiarize this library to send data for QR code rendering.</p>
<h2>Quick Example</h2>
<div class="post-section-highlight" readability="35">
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once '../vendor/autoload.php'; use chillerlan\QRCode\QRCode; // Core class for generating the QR code
$qrCode = new QRCode(); // data for which the QR code will be generated
$data = 'www.phppot.com'; // QR code image generation using render function
// it returns the an image resource.
$qrCodeImage = $qrCode-&gt;render($data); // Show the generated QR code image on screen
// following header is necessary to show image output
// in the browser
header('Content-Type: image/png');
imagepng($qrCodeImage);
imagedestroy($qrCodeImage);
</code></pre>
</div>
<p>The above code is a quick example of generating a Chillerlan QR code. You should use Composer to download the chillerlan dependency.</p>
<p>This example imports the library class and gives the data to generate the QR code.</p>
<p>The render() function passes the data to the library with which it will output the QR code image. This output can be returned to a browser or can be saved as a file.</p>
<p>In a previous article, we learned <a href="https://phppot.com/php/how-to-generate-qr-code-in-php/">how to render the generated QR code to the browser</a>.</p>
<p><img decoding="async" loading="lazy" class="alignnone size-large wp-image-21073" src="https://phppot.com/wp-content/uploads/2023/06/chillerlan-php-qrcode-550x367.jpg" alt="chillerlan php qrcode" width="550" height="367" srcset="https://phppot.com/wp-content/uploads/2023/06/chillerlan-php-qrcode-550x367.jpg 550w, https://phppot.com/wp-content/uploads/2023/06/chillerlan-php-qrcode-300x200.jpg 300w, https://phppot.com/wp-content/uploads/2023/06/chillerlan-php-qrcode-768x512.jpg 768w, https://phppot.com/wp-content/uploads/2023/06/chillerlan-php-qrcode.jpg 1200w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h3>Download via composer</h3>
<p>Run the following command in your terminal to install this Chillerlan PHP library.</p>
<pre class="prettyprint"><code class="language-php">composer require chillerlan/php-qrcode</code></pre>
<p><img decoding="async" loading="lazy" class="alignnone size-full wp-image-21069" src="https://phppot.com/wp-content/uploads/2023/06/qrcode-project-structure.jpg" alt="qrcode project structure" width="334" height="241" srcset="https://phppot.com/wp-content/uploads/2023/06/qrcode-project-structure.jpg 334w, https://phppot.com/wp-content/uploads/2023/06/qrcode-project-structure-300x216.jpg 300w" sizes="(max-width: 334px) 100vw, 334px"></p>
<h2>Example 2 – How to configure size, EC level, scale</h2>
<p>More configurations help to adjust the QR code quality without affecting readability.&nbsp; The below parameters are used, which override the default configurations.</p>
<ul>
<li><b>The version</b> is to set the size of a QR code.</li>
<li><strong>ECC level</strong> to set the possible values(L, M, Q, H). It is the damage tolerance percentage. We have seen it when <a href="https://phppot.com/php/php-qr-code-generator-with-phpqrcode-library/">coding with phpqrcode library</a>.</li>
<li><strong>Scale</strong> sets the size of a QR code pixel. The maximum size increases the QR code’s quality.</li>
</ul>
<p>This library has the QROptions class to set the configurations explicitly. When initiating this class, the code below prepares an array of {version, eccLeverl …} options.</p>
<p>This QROptions instance generates the QRCode object to call the render() action handler. As in the above example, the render() uses the data and bundles it into the QR code binary.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once '../vendor/autoload.php'; use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions; // data to embed in the QR code image
$data = 'www.phppot.com'; // configuration options for QR code generation
// eccLevel - Error correction level (L, M, Q, H)
// scale - QR code pixe size
// imageBase64 - output as image resrouce or not
$options = new QROptions([ 'version' =&gt; 5, 'eccLevel' =&gt; QRCode::ECC_H, 'scale' =&gt; 5, 'imageBase64' =&gt; true, 'imageTransparent' =&gt; false, 'foregroundColor' =&gt; '#000000', 'backgroundColor' =&gt; '#ffffff'
]); // Instantiating the code QR code class
$qrCode = new QRCode($options); // generating the QR code image happens here
$qrCodeImage = $qrCode-&gt;render($data); header('Content-Type: image/png');
imagepng($qrCodeImage);
imagedestroy($qrCodeImage);
</code></pre>
<h2>Chillerlan PHP library</h2>
<p>This is one of the popular QR Code generators in PHP. It has clean and easily understandable code with proper modularity.</p>
<p>Some of its features are listed below. This feature list represents the capability of being a component of a PHP application.</p>
<h3>Features</h3>
<ul>
<li>Creates QR Codes with an improved Model, Version, ECC level, and more configuration</li>
<li>It supports encoding numeric, alphanumeric, 8-bit binary, and more.</li>
<li>It supports QR code output in GD, ImageMagick, SVG markup, and more formats.</li>
<li>It provides QR code readers using GD and ImageMagick libraries.</li>
</ul>
<h2>More about QR code</h2>
<p>Hereafter we will see more about the QR code and its evolution,&nbsp; advantages, and usage scenarios.</p>
<p>The QR code, or the quick response code, is a two-dimensional (2D) bar code. The linked article has the code <a href="https://phppot.com/php/how-to-create-barcode-generator-using-php/">to generate a barcode using PHP</a>.</p>
<p>The QR code is a Japanese invention for the automotive industry. Later it spreads to more domains. Some of the commonly used places are,</p>
<ul>
<li>Marketing</li>
<li>Linking to service providers</li>
<li>Information sharing</li>
<li>Online payments.</li>
</ul>
<p>It provides easy access to online information through digital scanners. The QR code contains encoded data that can be decoded with digital scanning. It shares the information, links the service provider or prompts for the payment initiation after scanning.</p>
<h2>Example usages of QR code generation and scanning</h2>
<ul>
<li>It shows payee details to ensure and allows one to enter the amount to make a <strong>mobile payment</strong>.</li>
<li>It facilitates storing location and contact details. It is for <strong>marking locations</strong> in the Google map while scanning.</li>
<li>When reading the QR code, the application will <strong>download v-cards</strong> using the contact details stored.</li>
<li>The app developing company shows QR codes in the app store to <strong>download mobile apps</strong>.</li>
</ul>
<p><a class="download" href="https://phppot.com/downloads/php/chillerlan-php-qrcode.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/chillerlan-php-qrcode/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2023/06/15/php-qr-code-generator-with-chillerlan-php-qrcode-library/