Generating dynamic QR code and save it as a “PNG” image is an important task for any developer. QR codes are playing important role in many industries like ecommerce, ticket booking, online payment or almost everywhere these days!.
QR codes (Quick Response Codes) are two-dimensional barcodes. It’s a visual label with information about the item that can be readable by scanning machines. you can attached to the item. QR Code looks like a black squares grid on a white background. Please refer below image:
Generating dynamic QR code using php
In this article, I will explain you all the steps about how you can generate dynamic QR code using php. I recently had a requirement to generate a QR code for event ticket and create a PDF with the QR code.
Suggested Read: How to Generate PDF from Mysql Database using PHP
There are various third-party libraries available for generating QR codes in PHP. I chose the PHP QR Code library. Because use of this library is very easy and perfect fit for my requirements.
I am going to share the code in this tutorial step by step.
Download Library:
Download the PHP QR library using the below link.
https://sourceforge.net/projects/phpqrcode/files/
After download, please extract the zip file. I created a folder named qrcode in the project folder and moved the extracted folder inside it.
Generate QR Code And Display In Browser:
Please find below The syntax for generating QR codes.
2 3 4 |
QRcode::png($text, $file, $ecc, $pixel_size, $frame_size); |
- $text => Mandatory parameter. Other parameters are optional. This parameters will be use to generate the QR code and store the information.
- $file => Location of the file to save the generated QR code.
- $ecc => Error correction capability of QR. Following values are used. L, M, Q, and H.
- $pixel_size => Pixel size of the QR code.
- $frame_size => Size of QR code. Values are used from 0 to 10.
Generate QR Code
Create a new PHP file named qr_generator.php inside the qrcode folder and paste the below code to generate QR code in the browser.
2 3 4 5 6 7 8 |
<?php require_once("phpqrcode/qrlib.php"); $item = "ID 23456"; QRcode::png($item); ?> |
The $text is a parameter that may be your item id, order id or item information.
Save QR Code As Image:
Now I will create a from to input the information and generate QR code as well as save the QR code as an image. I need to define the file location where QR code image will store. Use the below code to generate QR and save it as a png image.
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 38 39 40 41 42 43 44 45 |
<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> |
Now open your file in browser: http://loclhost:8080/qrcode/qr_generator.php
Note: Please check permission to the “images” folder why because mac by default do not allow to save the files.
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Wrapping Words!
Thanks for reading 🙏, I hope you found How to generate dynamic QR code in PHP tutorial helpful for your project. Keep learning!. If you face any problem – I am here to solve your problems.
Also Read: How to Create ZIP File using PHP
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co
[…] Suggested Read: How to generate dynamic QR code using PHP […]