A QR code (short for Quick Response) is a specific matrix barcode (or two-dimensional code), readable by dedicated QR barcode readers and camera phones. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be text, URL or other data.
QR code is pretty efficient in spreading messages to smartphone devices. You can use your iphone, ipad, android phone or other smartphones to scan the code anywhere, then you would be able to add contacts, calendar event, open an url, send an email or any other information stored in the code, without typing.
It’s very simple to generate a QR code using Google Chart API. I encapsulate it in a php class for easy use. The code is as follows:
- <?php
- /**
- * This class generate QRCode using the API provided by google.
- * Feel free to use or modify code, but please keep reference.
- * @author Qi Yang <yangqi86@gmail.com>
- * @version 1.0 (04/15/2011)
- * @link http://yangqi.us
- *
- */
- class QRCode {
- /**
- * Image size
- * @var string
- */
- private $_imageSize = '150x150';
- /**
- * Output encoding
- * @var string
- */
- private $_outputEncoding = 'UTF-8';
- /**
- * Error correction level
- * @var string
- */
- private $_errorCorrectionLevel = 'L';
- /**
- * Image margin
- * @var string
- */
- private $_margin = '0';
- /**
- * Google qrcode api url
- * @var string
- */
- private $_apiUrl = 'http://chart.googleapis.com/chart';
- /**
- * The data to encode
- * @var string
- */
- private $_data = '';
- /**
- * Constructor calls config functions {@link
- */
- function __construct($config = array()) {
- $this->setImageSize($config['imageSize']);
- $this->setOutputEncoding($config['outputEncoding']);
- $this->setErrorCorrectionLevel($config['errorCorrectionLevel']);
- $this->setMargin($config['margin']);
- }
- /**
- * Set image size of the qrcode
- *
- * @param $imageSize
- * @return NULL
- */
- public function setImageSize($imageSize) {
- isset($imageSize) ? $this->_imageSize = $imageSize : NULL;
- }
- /**
- * Set output encoding of the qrcode
- *
- * @param $outputEncoding
- * @return NULL
- */
- public function setOutputEncoding($outputEncoding) {
- isset($outputEncoding) ? $this->_outputEncoding = $outputEncoding : NULL;
- }
- /**
- * Set error correction level of the qrcode
- *
- * @param $errorCorrectionLevel
- * @return NULL
- */
- public function setErrorCorrectionLevel($errorCorrectionLevel) {
- isset($errorCorrectionLevel) ? $this->_errorCorrectionLevel = $errorCorrectionLevel : NULL;
- }
- /**
- * Set image margin of the qrcode
- *
- * @param $margin
- * @return NULL
- */
- public function setMargin($margin) {
- isset($margin) ? $this->_margin = $margin : NULL;
- }
- /**
- * Set data to be encoded in the qrcode
- *
- * @param $data
- * @return NULL
- */
- public function setData($data) {
- isset($data) ? $this->_data = urlencode($data) : NULL;
- }
- /**
- * Get encoded qrcode image link string
- *
- * @return string
- */
- public function getLink() {
- return $this->_apiUrl . '?cht=qr&chs=' . $this->_imageSize
- . '&chl=' . $this->_data
- . '&choe=' . $this->_outputEncoding
- . '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
- }
- /**
- * Get encoded qrcode image by string
- *
- * @return string
- */
- public function getImage() {
- $data = 'cht=qr&chs=' . $this->_imageSize
- . '&chl=' . $this->_data
- . '&choe=' . $this->_outputEncoding
- . '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $this->_apiUrl);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- $response = curl_exec($ch);
- if( curl_errno($ch) ) {
- echo curl_error($ch);
- }
- curl_close($ch);
- return $response;
- }
- /**
- * Output qrcode image to the browser
- */
- public function render() {
- header('Content-type: image/png');
- echo $this->getImage();
- }
- }
- ?>
<?php
/**
* This class generate QRCode using the API provided by google.
* Feel free to use or modify code, but please keep reference.
* @author Qi Yang <yangqi86@gmail.com>
* @version 1.0 (04/15/2011)
* @link http://yangqi.us
*
*/
class QRCode {
/**
* Image size
* @var string
*/
private $_imageSize = '150x150';
/**
* Output encoding
* @var string
*/
private $_outputEncoding = 'UTF-8';
/**
* Error correction level
* @var string
*/
private $_errorCorrectionLevel = 'L';
/**
* Image margin
* @var string
*/
private $_margin = '0';
/**
* Google qrcode api url
* @var string
*/
private $_apiUrl = 'http://chart.googleapis.com/chart';
/**
* The data to encode
* @var string
*/
private $_data = '';
/**
* Constructor calls config functions {@link
*/
function __construct($config = array()) {
$this->setImageSize($config['imageSize']);
$this->setOutputEncoding($config['outputEncoding']);
$this->setErrorCorrectionLevel($config['errorCorrectionLevel']);
$this->setMargin($config['margin']);
}
/**
* Set image size of the qrcode
*
* @param $imageSize
* @return NULL
*/
public function setImageSize($imageSize) {
isset($imageSize) ? $this->_imageSize = $imageSize : NULL;
}
/**
* Set output encoding of the qrcode
*
* @param $outputEncoding
* @return NULL
*/
public function setOutputEncoding($outputEncoding) {
isset($outputEncoding) ? $this->_outputEncoding = $outputEncoding : NULL;
}
/**
* Set error correction level of the qrcode
*
* @param $errorCorrectionLevel
* @return NULL
*/
public function setErrorCorrectionLevel($errorCorrectionLevel) {
isset($errorCorrectionLevel) ? $this->_errorCorrectionLevel = $errorCorrectionLevel : NULL;
}
/**
* Set image margin of the qrcode
*
* @param $margin
* @return NULL
*/
public function setMargin($margin) {
isset($margin) ? $this->_margin = $margin : NULL;
}
/**
* Set data to be encoded in the qrcode
*
* @param $data
* @return NULL
*/
public function setData($data) {
isset($data) ? $this->_data = urlencode($data) : NULL;
}
/**
* Get encoded qrcode image link string
*
* @return string
*/
public function getLink() {
return $this->_apiUrl . '?cht=qr&chs=' . $this->_imageSize
. '&chl=' . $this->_data
. '&choe=' . $this->_outputEncoding
. '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
}
/**
* Get encoded qrcode image by string
*
* @return string
*/
public function getImage() {
$data = 'cht=qr&chs=' . $this->_imageSize
. '&chl=' . $this->_data
. '&choe=' . $this->_outputEncoding
. '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_apiUrl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$response = curl_exec($ch);
if( curl_errno($ch) ) {
echo curl_error($ch);
}
curl_close($ch);
return $response;
}
/**
* Output qrcode image to the browser
*/
public function render() {
header('Content-type: image/png');
echo $this->getImage();
}
}
?>