Home » PHP » PHP Qr Code Generator Example

About Olayemi Odunayo

Olayemi Odunayo
I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.

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:

  • A computer with PHP >= 5.5 installed
  • notepad++
  • PHP QRcode library

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!

 

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:

  • Supports QR Code versions (size) 1-40
  • Numeric, Alphanumeric, 8-bit and Kanji encoding.
  • Implemented purely in PHP, no external dependencies except GD2
  • Exports to PNG, JPEG images, also exports as bit-table
  • TCPDF 2-D barcode API integration
  • Easy to configure
  • Data cache for calculation speed-up
  • Provided merge tool helps deploy library as a one big dependency-less file, simple to “include and do not wory”
  • Debug data dump, error logging, time benchmarking
  • API documentation
  • Detailed examples
  • 100% Open Source, LGPL Licensed

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 

Do you want to know how to develop your skillset to become a Web Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. Building web apps with Node.js

2. HTML5 Programming Cookbook

3. CSS Programming Cookbook

4. AngularJS Programming Cookbook

5. jQuery Programming Cookbook

6. Bootstrap Programming Cookbook

 

and many more ....

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook