In this article i will show how to process QR codes in PHP and how to generate QR code images and scan those images back to their original text.
Â
Overview
QR codes is a standard that represents some decoded text often used with commercial items such as products and it used to identify those items in graphical format in the form of images. There are a lot of software that can generate and scan QR codes and in this tutorial we will show you how to manipulate QR codes with PHP.
For the purpose of this tutorial we will use two libraries that can be used to generate and scan QR codes, which are:
- phpqrcode: we will use this to generate QR codes, you can download it from this link.
- php-qrcode-detector-decoder: we will use to scan QR codes install it from this link.
After downloading the two libraries add them to your project, in the next sections we will see how to generate QR codes.
Generating QR codes:
To generate QR codes you need first to include the qrlib.php in your project, next create a new file generate.php as shown below:
<?php include './phpqrcode/qrlib.php';
Â
Output QR codes to the browser:
To output QR codes to the browser directly call Qrcode::png() as shown:
<?php include './phpqrcode/qrlib.php'; QRcode::png('web mob tuts');
Now run your script in the browser, and check the image shown.
Passing the output to img src:
Instead of outputting the result directly into the browser you can use the it as the src of the img tag as shown here:
<img src="./generate.php" />
Output QR code according to parameter:
You can pass parameters to the url therapy you can capture those parameters and output QR codes accordingly as shown below:
<?php include './phpqrcode/qrlib.php'; $id = $_GET['id']; $code = "web mob tuts - $id"; QRcode::png($code);
Also you can pass parameters to url in the img src:
<img src="./generate.php?id=3" />
Saving the Qr code to a file:
You can if you want to save the QR code to file or store it in the database, just pass a second parameter to QRcode::png() which is the file path to store in the png as shown here:
include './phpqrcode/qrlib.php'; $code = "web mob tuts"; $fileName = md5(uniqid()).'.png'; $tempDir = "files"; // the directory to store the files $filePath = $tempDir . "/" . $fileName; QRcode::png($code, $filePath); // note the second parameter if(file_exists($filePath)) { echo '<img src="./'.$filePath.'" />'; }
Controlling pixel size:
You can control the pixel size when creating the QR code by passing a third and fourth parameters, the third parameter represents the ECC level and the fourth parameter represents the pixel size, 1 small size and 4 biggest size:
$code = 'web mob tuts'; QRcode::png($code, 'files/1.png', QR_ECLEVEL_L, 1); QRcode::png($code, 'files/2.png', QR_ECLEVEL_M, 2); QRcode::png($code, 'files/3.png', QR_ECLEVEL_Q, 3); QRcode::png($code, 'files/4.png', QR_ECLEVEL_H, 4); // displaying echo '<img src="files/1.png" />'; echo '<img src="files/2.png" />'; echo '<img src="files/3.png" />'; echo '<img src="files/4.png" />';
Reading QR codes:
To read or scan Qr code we will use the second library, so include the dependencies from the vendor autoload file as follows:
<?php require "./vendor/autoload.php"; use Zxing\QrReader;
As shown above we included the vendor composer file and then we imported the namespace relating the class QrReader, not that without this line you will get a fatal error.
To read specific Qr codes just pass the image png file to read method as shown below:
$qrcode = new QrReader('files/1.png'); $text = $qrcode->text(); echo $text;
Calling $qrcode->text() will retrieve the QR code data.
QR code can be easily created in PHP using some best php qr code library. Found one article which has explain each step to generate QR code in php here : https://www.discussdesk.com/how-to-generate-qr-code-using-php.htm Here you can see live demo and download the working script.