QR Code Generate Class in PHP

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:

  1. <?php
  2. /**
  3.  * This class generate QRCode using the API provided by google.
  4.  * Feel free to use or modify code, but please keep reference.
  5.  * @author Qi Yang <yangqi86@gmail.com>
  6.  * @version 1.0 (04/15/2011)
  7.  * @link http://yangqi.us
  8.  *
  9.  */
  10. class QRCode {
  11.  
  12.     /**
  13.      * Image size
  14.      * @var string
  15.      */
  16.     private $_imageSize = '150x150';
  17.     /**
  18.      * Output encoding
  19.      * @var string
  20.      */
  21.     private $_outputEncoding = 'UTF-8';
  22.     /**
  23.      * Error correction level
  24.      * @var string
  25.      */
  26.     private $_errorCorrectionLevel = 'L';
  27.     /**
  28.      * Image margin
  29.      * @var string
  30.      */
  31.     private $_margin = '0';
  32.     /**
  33.      * Google qrcode api url
  34.      * @var string
  35.      */
  36.     private $_apiUrl = 'http://chart.googleapis.com/chart';
  37.     /**
  38.      * The data to encode
  39.      * @var string
  40.      */
  41.     private $_data = '';
  42.  
  43.     /**
  44.      * Constructor calls config functions {@link
  45.      */
  46.     function __construct($config = array()) {
  47.         $this->setImageSize($config['imageSize']);
  48.         $this->setOutputEncoding($config['outputEncoding']);
  49.         $this->setErrorCorrectionLevel($config['errorCorrectionLevel']);
  50.         $this->setMargin($config['margin']);
  51.     }
  52.  
  53.     /**
  54.      * Set image size of the qrcode
  55.      *
  56.      * @param $imageSize
  57.      * @return NULL
  58.      */
  59.     public function setImageSize($imageSize) {
  60.         isset($imageSize) ? $this->_imageSize = $imageSize : NULL;
  61.     }
  62.     /**
  63.      * Set output encoding of the qrcode
  64.      *
  65.      * @param $outputEncoding
  66.      * @return NULL
  67.      */
  68.     public function setOutputEncoding($outputEncoding) {
  69.         isset($outputEncoding) ? $this->_outputEncoding = $outputEncoding : NULL;
  70.     }
  71.     /**
  72.      * Set error correction level of the qrcode
  73.      *
  74.      * @param $errorCorrectionLevel
  75.      * @return NULL
  76.      */
  77.     public function setErrorCorrectionLevel($errorCorrectionLevel) {
  78.         isset($errorCorrectionLevel) ? $this->_errorCorrectionLevel = $errorCorrectionLevel : NULL;
  79.     }
  80.     /**
  81.      * Set image margin of the qrcode
  82.      *
  83.      * @param $margin
  84.      * @return NULL
  85.      */
  86.     public function setMargin($margin) {
  87.         isset($margin) ? $this->_margin = $margin : NULL;
  88.     }
  89.     /**
  90.      * Set data to be encoded in the qrcode
  91.      *
  92.      * @param $data
  93.      * @return NULL
  94.      */
  95.     public function setData($data) {
  96.         isset($data) ? $this->_data = urlencode($data) : NULL;
  97.     }
  98.  
  99.     /**
  100.      * Get encoded qrcode image link string
  101.      *
  102.      * @return string
  103.      */
  104.     public function getLink() {
  105.         return $this->_apiUrl . '?cht=qr&chs=' . $this->_imageSize
  106.             . '&chl=' . $this->_data
  107.             . '&choe=' . $this->_outputEncoding
  108.             . '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
  109.     }
  110.  
  111.     /**
  112.      * Get encoded qrcode image by string
  113.      *
  114.      * @return string
  115.      */
  116.     public function getImage() {
  117.         $data = 'cht=qr&chs=' . $this->_imageSize
  118.             . '&chl=' . $this->_data
  119.             . '&choe=' . $this->_outputEncoding
  120.             . '&chld=' . $this->_errorCorrectionLevel . '|' . $this->_margin;
  121.         $ch = curl_init();
  122.         curl_setopt($ch, CURLOPT_URL, $this->_apiUrl);
  123.         curl_setopt($ch, CURLOPT_POST, TRUE);
  124.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  125.         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  126.         curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
  127.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  128.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  129.         $response = curl_exec($ch);
  130.         if(  curl_errno($ch) ) {
  131.             echo curl_error($ch);
  132.         }
  133.         curl_close($ch);
  134.  
  135.         return $response;
  136.     }
  137.  
  138.     /**
  139.      * Output qrcode image to the browser
  140.      */
  141.     public function render() {
  142.         header('Content-type: image/png');
  143.         echo $this->getImage();
  144.     }
  145.  
  146. }
  147.  
  148. ?>
<?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();
	}

}

?>
This entry was posted in PHP and tagged , , , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">