Generate QR Code Using PHP

Bharathiraja
May 6 · 3 min read

Generate and Save the QR Code as a PNG image.

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 Library:

Download the QR library using the below link.

https://sourceforge.net/projects/phpqrcode/files/

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);
  1. $text => Mandatory parameter. Other parameters are optional. The data to be stored in the QR code is given in this field.
  2. $file => Location of the file to save the generated QR code.
  3. $ecc => Error correction capability of QR. Following values are used. L, M, Q, and H.
  4. $pixel_size => Pixel size of the QR code.
  5. $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.

Summary:

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:

http://phpqrcode.sourceforge.net/

Further Reading.

Ampersand Academy

Ampersand Academy offers classroom & online training for…

Bharathiraja

Written by

Deep&Machine Learning, Ionic, Full Stack Developer. https://ampersandacademy.com/tutorials/ https://github.com/bharathirajatut

Ampersand Academy

Ampersand Academy offers classroom & online training for Data Analytics using SAS, R & Python, Data Science using R & Python, Deep Learning, Ionic, & Tableau. We also offer free tutorials and plugins. Write for us. Mail your article link to bharathirajatut@gmail.com

Bharathiraja

Written by

Deep&Machine Learning, Ionic, Full Stack Developer. https://ampersandacademy.com/tutorials/ https://github.com/bharathirajatut

Ampersand Academy

Ampersand Academy offers classroom & online training for Data Analytics using SAS, R & Python, Data Science using R & Python, Deep Learning, Ionic, & Tableau. We also offer free tutorials and plugins. Write for us. Mail your article link to bharathirajatut@gmail.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store