How to generate QR code in PHP
Generating dynamic QR codes using PHP involves using a library to handle the QR code generation. One popular library for this purpose is PHP QR Code. Here’s a step-by-step guide on how to generate dynamic QR codes using PHP:
Download PHP QR Code Library (phpqrcode):
https://sourceforge.net/projects/phpqrcode/
Include the phpqrcode
library in the project folder
Now create a HTML form:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div class="col-lg-12"> <div class="panel panel-primary"> <div class="panel-heading">Enter information to generate QR Code</div> <div class="panel-body"> <div class="input_field_wrapper"> <div> <form method="post"> <input type="text" name="item_id" value="" required /> <input type="submit" class=" btn btn-primary" value="Generate QR Code" style=" margin:5px;"> </form> </div> </div> </div></div></div> |
Code for generating the QR code in the browser
1 2 |
QRcode::png($text, $file, $ecc, $pixel_size, $frame_size); |
QRcode::png()
appears to be a function call in a programming language, possibly PHP, used for generating a PNG image containing a QR code. Let’s break down the parameters:
$text
: This is the text or data that you want to encode into the QR code. It could be a URL, a piece of text, or any other data that you want to make accessible via scanning the QR code.$file
: This parameter likely specifies the file path where the generated PNG image will be saved. After calling this function, a PNG file containing the QR code will be created at this location.$ecc
: This stands for Error Correction Capability. It determines the level of error correction used in the QR code. Higher error correction allows the QR code to still be readable even if it’s partially obscured or damaged. It typically has values like ‘L’, ‘M’, ‘Q’, or ‘H’, indicating different levels of error correction (L being the lowest and H being the highest).$pixel_size
: This specifies the size of each pixel in the QR code image. It determines the resolution of the QR code.$frame_size
: This parameter sets the size of the white border around the QR code. It ensures that the code is adequately framed and doesn’t touch the boundaries of the image.
So, when you call QRcode::png($text, $file, $ecc, $pixel_size, $frame_size);
, it generates a PNG image containing a QR code based on the provided parameters and saves it to the specified file path.
PHP Code for generating QR code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php //load the ar library include 'phpqrcode/qrlib.php'; if(isset($_POST['item_id'])){ //data to be stored in qr $item =$_POST['item_id']; //file path $file = "images/qr1.png"; //other parameters $ecc = 'H'; $pixel_size = 20; $frame_size = 5; // Generates QR Code and Save as PNG QRcode::png($item, $file, $ecc, $pixel_size, $frame_size); // Displaying the stored QR code if you want echo "<div><h3>Generated QR Code for {".$item."} </h3><br><img src='".$file."' width='150'></div>"; } ?> |
Here is the full code written in this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<div class="content"> <div class="col-lg-12"> <div class="panel panel-primary"> <div class="panel-heading">Enter information to generate QR Code</div> <div class="panel-body"> <div class="input_field_wrapper"> <div> <form method="post"> <input type="text" name="item_id" value="" required /> <input type="submit" class=" btn btn-primary" value="Generate QR Code" style=" margin:5px;"> </form> </div> </div> </div></div></div> <div class="col-lg-12"> <?php //load the ar library include 'phpqrcode/qrlib.php'; if(isset($_POST['item_id'])){ //data to be stored in qr $item =$_POST['item_id']; //file path $file = "images/qr1.png"; //other parameters $ecc = 'H'; $pixel_size = 20; $frame_size = 5; // Generates QR Code and Save as PNG QRcode::png($item, $file, $ecc, $pixel_size, $frame_size); // Displaying the stored QR code if you want echo "<div><h3>Generated QR Code for {".$item."} </h3><br><img src='".$file."' width='150'></div>"; } ?> </div> </div> |