PHP

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

 
 

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 

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button