In this tutorial, we are going to see how to generate the QR code using PHP. Recently I had a requirement to generate a QR code and create a PDF with the QR code. I am sharing the code in this tutorial.
There are many third-party libraries are available to generate the QR code using PHP. I chose the PHP QR Code library. My requirement is very simple. This library is a perfect match for my requirement. Later I will try other PHP QR code libraries and will share those codings too.
The codes are tested using PHP 7.2 on macOS. We will see how to generate the QR code in step by step example.
Download the QR library using the below link.
After download, please extract the zip file and paste it inside the htdocs folder. I created a folder named qrcode in the htdocs folder and pasted the extracted folder inside it.
Generate QR Code And Display In Browser:
The syntax for generating QR codes is given below.
QRcode::png($text, $file, $ecc, $pixel_size, $frame_size);
- $text => Mandatory parameter. Other parameters are optional. The data to be stored in the QR code is given in this field.
- $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.
Create a new PHP file named generate-qr.php inside the qrcode folder and paste the below code to generate QR code in the browser.
The $text parameter is only mandatory. If you run the above code, it will generate the QR code and will display it in the browser.
Save QR Code As Image:
To save the QR code as an image we need to give the file location. Use the below code to generate QR and save it as a png image.
Give permission to the folder why because mac by default do not allow to save the files.
QR code is very popular in many industries. This tutorial explains how to download the QR code library for PHP and how to generate the QR code using the PHP library. It is very easy, simple, and straightforward.
This QR code is used for product billing(my requirement). We need to paste the QR code on the product. So I created the QR code and created a PDF using this QR code. ( 5 columns QR code in a row for printing). I used mPDF to generate the PDF.
I hope this article will be very useful.
Thank you for reading.
QR Library Link:
How To Save and Verify Password Using PHP and MySQL
Use password_hash() and password_verify() function.
PHP Read and Write Excel File Using PhpSpreadsheet
Recently I had a requirement in PHP to read and write an excel file.