PHP Qr Code Generator Example

QR codes is an abbreviation for quick response code. It was first designed for the automotive industry in Japan. A QR code consists of black squares arranged in a square grid on a white background. QR codes is used to store information or data. Data stored with QR codes can be read with a smart mobile device. In this example we will describe how to generate QR codes with PHP for your website.
For this example we will use:

 
 

Want to be a PHP master?
Subscribe to our newsletter and download the PHP programming Cookbook right now!
In order to get you familiarized with PHP development, we have compiled a comprehensive guide about all major aspects of PHP programming. The perfect tool to boost your WEB development career. Besides reading them online you may download the eBook in PDF format!
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

1. Getting Started

To generate QR codes with PHP we will use an open source library called PHP QR code. We use this library to generate QR codes. You can get it from here.
According to library website PHP QR Code is an open source (LGPL) library for generating QR Code, 2-dimensional barcode. Based on libqrencode C library, provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2). Implemented purely in PHP.
Some of the features of this library are:

PHP QR code library makes it very easy to generate QR codes in PHP. After downloading the library from the link given above you should extract it (since it’s in a zip ) to a suitable place in your hard drive ( most likely where your PHP scripts are stored). Your file path would look something like what is below.

\var\www\phpqrcode
\var\www\your_app

PHP QR code library doesn’t need any external dependency except GD2. If you encounter any problems while using this library or you just want to check before hand, you should create a script with the phpinfo() keyword, to get all the details about your PHP installation.

<?php
phpinfo();
>

index.php

<?php
include "phpqrcode/qrlib.php";
// create a QR Code with this text and display it
QRcode::png("MY FIRST PHP QR CODE GENERATOR EXAMPLE") ;
?>

The first script above, is very simple. It generates a QR code and displays it in the browser. We only supply a string as an argument to the method QRcode::png . The string supplied is the information our qr code would hold or store. It’s so easy. Withonly  just two lines of code we have  already created a working example.
Lets look at another example:

index2.php

<?php
include "phpqrcode/qrlib.php";
QRcode::png ("http:www.webcodegeeks.com " , "test.png ", "L", 5, 5) ;
?>

The second example might seem rather complex- but it is not really. The first parameter supplied to the method ( http:www.webcodegeeks.com ) represents the information to be encoded, the second parameter, has a default value of false meaning the image is displayed by the browser. Here we set the value to a valid file name ( test.png), this tells the QR code library to create a file containing the QR code.

The third parameter is the level of error correction for the generated barcode, passed as a single letter string. This specifies how much of the data’s codewords (8-bits per codeword) can be restored for a distorted or damaged QR Code image using the Reed-Solomon error correction algorithm. The higher the correction level, the less the data capacity of the barcode can be for a given dimension.

The fourth parameter supplied, defines the size of each of the barcode squares and it is in pixels. In our example we specified five parameters so we have a 5 by 5 barcode square.
The fifth parameter defines the white margin boundary around the barcode. To preview the QR code we just created is simple. Lets do that in our next example.

index2.php

>!DOCTYPE html< 
>html lang=en<
	>head<
	>style<
	>/style<
		>meta charset="utf-8" /<
		>meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/<
		>title<
Previewing The QR Code Image
>/title<
		
		>/head<
	>body<
	>img src="test.png"<
	>/body<
	>/html<

QR code is commonly used in advertising, packaging, commercial tracking, entertaining, transport ticketing, product marketing and in-store product labeling. Besides, it can also be applied to store personal information by government. Moreover, it may appear in magazines, on signs, buses, business cards, or almost any object that users might need information.

2. Summary

In this example we learnt about QR codes, what they are and their uses. We learnt how to create QR codes in PHP, through the use of QR code library.

3. Download the source code

Download
You can download the full source code of this example here: phpqrcodegenerator 
Exit mobile version