How To Generate QR Code in PHP Using Library Tutorial

Reading Time: 6 minutes
4,278 Views

Inside this article we will see the concept i.e How To generate QR Code in PHP Using Library. “phpqrcode” is a PHP library which can be used to generate dynamic qr codes.

A QR code is a type of matrix barcode invented in 1994 by the Japanese automotive company Denso Wave. A barcode is a machine-readable optical label that can contain information about the item to which it is attached.

We will generate QR code by using library. By using this concept you can create dynamic QR code. Additionally using this qrcode php library, you can customize as per your requirements of QR Code like size, color, bgcolor, margin, format etc.

Learn More –

Let’s get started.

Download QrCode PHP Library

There are number of PHP library available to generate qrcodes. But we will use “phpqrcode” one of the best library from it.

You can click on it. It will redirect you to a github repository. Only you need to clone it or download it inside your project directory.

We will use this library and work here for two cases – Generate a simple Qr code and Generate a QR code with attributes and save image into a folder.

Let’s create a folder structure for it.

Application Folder Structure

Here,

We can see we have a folder i.e php-qrcode. Inside this folder we have two folders – images/ (Folder to save qrcode images) and phpqrcode/ (PHP qrcode library). And we have a file i.e index.php

Application Programming

We will work for two cases –

  • Generate a simple QRcode .
  • Generate a QRcode and save into images folder.

Case #1 – Generate a simple QRcode

Open index.php file and write this complete code into it.

<?php

// Include the qrlib file
include 'phpqrcode/qrlib.php';

// $text variable has data for QR
$text = "Online Web Tutor - Learn for Skills";

// QR Code generation using png()
// When this function has only the
// text parameter it directly
// outputs QR in the browser

QRcode::png($text);

When this executes, it will create a qrcode image into your output screen.

Case #2 – Generate a QRcode and save into images folder

In this case, not only we will generate qrcode also we will save this qrcode image into application folder.

Open index.php and write this code into it.

<?php
// Include the qrlib file
include 'phpqrcode/qrlib.php';

// $text variable has data for QR
$text = "Online Web Tutor - Learn for Skills";
  
// $path variable store the location where to 
// store image and $file creates directory name
// of the QR code file by using 'uniqid'
// uniqid creates unique id based on microtime
$path = 'images/';
$file = $path.uniqid().".png";
  
// $ecc stores error correction capability('L')
$ecc = 'L';
$pixel_Size = 5;
$frame_Size = 5;
  
// Generates QR Code and Stores it in directory given
QRcode::png($text, $file, $ecc, $pixel_Size, $frame_size);
  
// Displaying the stored QR code from directory
echo "<center><img src='".$file."'></center>";

When this executes, it will create a qrcode image into your output screen.

It will this generated qr code image into images/ application folder. Make sure images/ folder should have sufficient permission to read and write files into it.

Download Complete Source Code

We hope this article helped you to learn How To Generate QR Code in PHP Using Library Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more