PHP QrCode Generator

Introduction

PHP QrCode Generator is a QrCode script that generates QrCode images using the Google Chart API. The script can generate the QrCode URL, the QrCode Image tag (HTML) or the image itself. It's possible to customize the size, encoding and also the type of the QrCode. The script support Text, Email, SMS, Phone, MeCard, GeoLocation and Wifi QrCodes.

Setup

Copy the QrCode Generator file to a directory of your choice. In the page where you want to create QrCodes, include that file.

/**
 * Load the QrCode Generator File
 */
require ('qrcode.php');

Now you can create your QrCode object. First, start by initializing the QrCode Generator Class. It accepts 4 parameters, the last two are optional. The simplest QrCode type is the text type. The first parameter is the Qrcode type and the second is the type value. The value can be string, integer or an array depending on the type. The last two parameters are covered lately.

/**
 * Create the QrCode Generator File
 */
$qrcode = new qrcode('text', 'This is a sample QrCode');
/**
 * Display the Generated QrCode URL
 */
echo $qrcode;
/**
 * Display the Generated QrCode Image
 */
echo $qrcode->to_image();

Customizing the QrCode

QrCode Size: You can change your QrCode height and width size. It's recommanded, however, to keep both these values equal. The default values are 250x250. The QrCode size parameter is an associative array that accepts two values: height and width.
QrCode Encoding: How to encode the data. The available values are UTF-8, Shift_JIS and ISO-8859-1. The default value is UTF-8.

/**
 * Specifies the size and encoding parameters.
 */
$size = array('height'=>120, 'width'=>120);
$encoding = 'UTF-8';
/**
 * Size and Encoding are optional. Size is the third parameter and encoding is the last.
 */
$qrcode = new qrcode('text', 'This is a sample QrCode', $size, $encoding);
echo $qrcode->to_image();

Using it in your page

The following snippet of code shows how you can use the PHP QrCode Generator in a web page. The first step, as mentioned above, is to include the file. Next, we create the QrCode object using the qrocde.php class. Finally, we insert the QrCode image in the right place. This same code can be found in the sample folder in the file you downloaded.

PHP Code
/**
 * Load the QrCode Generator File
 */
require ('qrcode.php');

/**
 * Create an SMS QrCode with size 240x240 
 */
$size = array('height' => 240,
			  'width' => 240
			 );

$value = array( 'number' => 21698667534, 
				'message' => 'Hello!'
			  );

$qrcode = new qrcode('sms', $value);
HTML Code


  Sample QrCode Generator


  
<php echo $qrcode->to_image(); ?>

QrCode Types

Text: This is the most basic type. It accepts a string value.

/**
 * Text type: Accepts string as a value.
 */
$qrcode = new qrcode('text', 'This is a sample QrCode');
echo $qrcode->to_image();

Email: Creates an Email QrCode. The QrCode can hold the Email Address, as well as the body of the email.

/**
 * Email type: Accepts an array with email and body keys.
 */
$value = array( 'email' => 'contact@csomar.com', 
				'body' => 'Hello'
			  );
$qrcode = new qrcode('email', $value);
echo $qrcode -> to_image();

Call: Creates a QrCode with a Phone number to make a call.

/**
 * Phone type: Accepts an integer (Phone Number).
 */
$qrcode = new qrcode('phone', 21698667534);
echo $qrcode -> to_image();

SMS: Creates a QrCode that sends an SMS message to a phone number.

/**
 * SMS type: Accepts an Array with two keys: number (integer) and message (string)
 */
$value = array( 'number' => 21698667534, 
				'message' => 'Hello!'
			  );
$qrcode = new qrcode('sms', $value);
echo $qrcode;

MeCard: Creates a MeCard QrCode.

/**
 * MeCard
 */
$value = array( 'name' => 'Abid Omar', 
				'number' => 21698667534, 
				'email' => 'contact@omarabid.com', 
				'address' => 'Tunisia', 
				'memo' => 'Developer'
			  );
$qrcode = new qrcode('mecard', $value);
echo $qrcode;

BizCard: Creates a BizCard QrCode.

/**
 * BizCard
 */
$value = array( 'first_name' => 'Abid',
				'last_name' => 'Omar',
				'company' => 'Csomar',
				'job' => 'Developer',
				'address' => 'Home',
				'phone' => '021698667534',
				'email' =>'omar.abid2006@gmail.com',
				'website' =>'http://csomar.com'
			  );
$qrcode = new qrcode('bizcard', $value);
echo $qrcode->to_image();

PayPal: Creates a PayPal Buy Now button. This QrCode is an URL that redirects to a PayPal buy now page. The required settings are your PayPal ID (Email), Item Description, the price (amount), the quantity, the currency and the item code.

/**
 * PayPal QrCode
 */
$value = array( 'email' => 'pp@csomar.com',
				'description' => 'my item',
				'amount' => 120,
				'currency' => 'USD',
				'quantity' => 5,
				'item_number' => 101
			  );
$qrcode = new qrcode('bizcard', $value);
echo $qrcode->to_image();

Location: Creates a GeoLocation QrCode. You need the Latitude and Longitude attributes which can be located with Google Maps.

/**
 * Location type: Accepts an Array with keys Latitude (decimal) and Longitude (decimal).
 */
$value = array( 'latitude' => 2.453, 
				'longitude' => 1.564
			  );
$qrcode = new qrcode('location', $value);
echo $qrcode;

Wifi: Creates a QrCode to be read by phones and connect to your Wifi network.

/**
 * Wifi type: Accepts an Array with the SSID, type and password of the Wifi Spot.
 */
$value = array( 'SSID' => 'my_network', 
				'network_type' => 'WEP', 
				'password' => 'cafe-wifi'
			  );
$qrcode = new qrcode('wifi', $value);
echo $qrcode;

Bookmark: Creates a QrCode to be read by phones and bookmark your page.

/**
 * Bookmark type: Accepts an Array with Title and URL of your page.
 */
$value = array( 'title' => 'my page', 
				'url' => 'http://url.com'
			  );
$qrcode = new qrcode('bookmark', $value);
echo $qrcode;