QR Code Generation Library for Codeignitor

QR Code generator in Codeignitor

Codeigniter PHP QR Code

Original code author https://github.com/dwisetiyadi/CodeIgniter-PHP-QR-Code I have made some changes to fit into my CI file system

Introduction

This a library for CodeIgniter Framework to make QR Code from some string given, a porting code from http://phpqrcode.sourceforge.net/.

Installation and Requirements

This library requires CodeIgniter Framework and GD2 PHP extension.

How to use

Copy the Ci_qr_code.php into YOUR_CODEIGNITOR_DIR/application/libraries

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * PHP QR Code porting for Codeigniter
 *
 * @package        	CodeIgniter
 * @subpackage    	Libraries
 * @category    	Libraries
 * @porting author	dwi.setiyadi@gmail.com
 * @original author	http://phpqrcode.sourceforge.net/
 * @link 			https://github.com/dwisetiyadi/CodeIgniter-PHP-QR-Code
 * @version		1.0	
 * @license 
 */
 
class Ci_qr_code
{
	private $_cacheable 		= TRUE;
	private $_cachedir 			= 'tmp/cache/';
	private $_error_log_dir 	= 'tmp/logs/';
	private $_ci_qr_code_lib 	= 'application/third_party/qr_code/';
	private $_quality 			= TRUE;
	private $_size 				= 1024;
	

	/**
	 * Constructor
	 */
	public function __construct($config = array()) 
	{
		$this->initialize($config);
	}

	/** 
	 * initialize  
	 * Default Method to initailize the QR Code Library
	 * 
	 * @access public 
	 * @param  array
	 * @return   
	 */

	public function initialize($config = array()) 
	{
		$this->_cacheable 		= (isset($config['cacheable'])) ? $config['cacheable'] : $this->_cacheable;
		$this->_cachedir 		= (isset($config['cachedir'])) ? $config['cachedir'] : FCPATH.$this->_cachedir;
		$this->_error_log_dir 	= (isset($config['errorlog'])) ? $config['errorlog'] : FCPATH.$this->_error_log_dir;
		$this->_ci_qr_code_lib 	= (isset($config['ciqrcodelib'])) ? $config['ciqrcodelib'] : FCPATH.$this->_ci_qr_code_lib;
		$this->_quality 		= (isset($config['quality'])) ? $config['quality'] : $this->_quality;
		$this->_size 			= (isset($config['size'])) ? $config['size'] : $this->_size;
		
		// use cache - more disk reads but less CPU power, masks and format templates are stored there
		if ( ! defined('QR_CACHEABLE')) 
		{
			define('QR_CACHEABLE', $this->_cacheable);
		}
		
		// used when QR_CACHEABLE === true
		if ( ! defined('QR_CACHE_DIR')) 
		{
			define('QR_CACHE_DIR', $this->_cachedir);
		}
		
		// default error logs dir
		if ( ! defined('QR_LOG_DIR')) 
		{
			define('QR_LOG_DIR', $this->_error_log_dir);
		}
		
		// if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
		if ($this->_quality) 
		{
			if ( ! defined('QR_FIND_BEST_MASK')) 
			{
				define('QR_FIND_BEST_MASK', TRUE);
			}
		} 
		else {
			if ( ! defined('QR_FIND_BEST_MASK')) 
			{	
				define('QR_FIND_BEST_MASK', FALSE);
			}
			if ( ! defined('QR_DEFAULT_MASK')) 
			{		
				define('QR_DEFAULT_MASK', $this->_quality);
			}
		}
		
		// if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
		if ( ! defined('QR_FIND_FROM_RANDOM')) 
		{
			define('QR_FIND_FROM_RANDOM', FALSE);
		}
		
		// maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images
		if ( ! defined('QR_PNG_MAXIMUM_SIZE')) 
		{
			define('QR_PNG_MAXIMUM_SIZE',  $this->_size);
		}
	
		// call original library
		include_once($this->_ci_qr_code_lib."qrconst.php");
		include_once($this->_ci_qr_code_lib."qrtools.php");
		include_once($this->_ci_qr_code_lib."qrspec.php");
		include_once($this->_ci_qr_code_lib."qrimage.php");
		include_once($this->_ci_qr_code_lib."qrinput.php");
		include_once($this->_ci_qr_code_lib."qrbitstream.php");
		include_once($this->_ci_qr_code_lib."qrsplit.php");
		include_once($this->_ci_qr_code_lib."qrrscode.php");
		include_once($this->_ci_qr_code_lib."qrmask.php");
		include_once($this->_ci_qr_code_lib."qrencode.php");
	}
	/** 
	 * generate  
	 * Method to generate the qr code
	 * 
	 * @access public 
	 * @param  array
	 * @return   
	 */
	public function generate($params = array()) 
	{
		if(isset($params['black']) 
			&& is_array($params['black']) 
			&& count($params['black']) == 3 
			&& array_filter($params['black'], 'is_int') === $params['black']) {
			QRimage::$black = $params['black']; 
		}
		
		if(isset($params['white']) 
			&& is_array($params['white']) 
			&& count($params['white']) == 3 
			&& array_filter($params['white'], 'is_int') === $params['white']) {
			QRimage::$white = $params['white']; 
		}
		
		$params['data'] = (isset($params['data'])) ? $params['data'] : 'QR Code Library';


		$level = 'L';
		if(isset($params['level']) && in_array($params['level'], array('L','M','Q','H'))) 
		{
			$level = $params['level'];
		}
		
		$size = 4;
		if(isset($params['size'])) 
		{
			$size = min(max((int)$params['size'], 1), 10);
		}
		
		if(isset($params['savename'])) 
		{ 	
			QRcode::png($params['data'], $params['savename'], $level, $size, 2);
			return $params['savename'];
		} 
		else 
		{
			QRcode::png($params['data'], NULL, $level, $size, 2);
		}
	}
}
// END Ci_qr_code Class
/* End of file Ci_qr_code.php */
/* Location: ./application/libraries/Ci_qr_code.php */

Copy the qr_code.php into YOUR_CODEIGNITOR_DIR/application/config

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * PHP QR Code porting for Codeigniter
 *
 * @package        	CodeIgniter
 * @subpackage    	Libraries
 * @category    	Libraries
 * @porting author	dwi.setiyadi@gmail.com
 * @original author	http://phpqrcode.sourceforge.net/
 * @link 			https://github.com/dwisetiyadi/CodeIgniter-PHP-QR-Code
 * @version		1.0	
 * @license 
 */

 
$config['cacheable'] 	= TRUE; //boolean, the default is true
$config['cachedir'] 	= 'tmp/cache/'; //string, the default is tmp/cache/
$config['imagedir'] 	= 'tmp/qr_codes/'; //string, the default is tmp/qr_codes/
$config['errorlog'] 	= 'tmp/logs/'; //string, the default is tmp/logs/
$config['ciqrcodelib'] 	= 'application/third_party/qr_code/'; //string, the default is application/third_party/qr_code/
$config['quality'] 		= TRUE; //boolean, the default is true 
$config['size'] 		= 1024; 	//interger, the default is 1024
$config['black']        = array(224,255,255); // array, default is array(255,255,255)
$config['white']        = array(70,130,180); // array, default is array(0,0,0)
  
/* End of file qr_code.php */
/* Location: ./application/config/qr_code.php */

Note: You can make required changes as per your folder structure

QR Code Implementation

Copy the qr_code_generate.php Controller Class into YOUR_CODEIGNITOR_DIR/application/controllers

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Qr_code_generate extends CI_Controller {

	private $_controller_url 		= 'qr_code_generate/';
	private $_method_url 			= '';
	private $_form_attributes 		= array();

	public  $data 					= array();
	/**
	 * Constructor
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/** 
	 * index  
	 * Default Method to display locations dashboard
	 * 
	 * @access public 
	 * @param  
	 * @return   
	 */
	public function index()
	{ 		
		$this->_method_url = $this->_controller_url . 'index';  
		$this->_form_attributes['form_action'] = site_url(). $this->_method_url;
		$this->_form_attributes['hidden_fields'] = array('submitted' => TRUE);
		$this->load->helper('form'); 
		$this->_form_attributes['attributes'] = array('class' => 'form-horizontal', 'id' => 'frm_generate_qr_code', 'method' => 'post');
		$this->data['form_open'] = form_open($this->_form_attributes['form_action'], $this->_form_attributes['attributes'], $this->_form_attributes['hidden_fields']);
		if($this->input->post('submitted') === FALSE)
		{
			$this->load->view('qr_code_generate', $this->data); 
			
		}
		else
		{
			$this->load->library('ci_qr_code');
			$this->config->load('qr_code');
			$qr_code_config = array(); 
			$qr_code_config['cacheable'] 	= $this->config->item('cacheable');
			$qr_code_config['cachedir'] 	= $this->config->item('cachedir');
			$qr_code_config['imagedir'] 	= $this->config->item('imagedir');
			$qr_code_config['errorlog'] 	= $this->config->item('errorlog');
			$qr_code_config['ciqrcodelib'] 	= $this->config->item('ciqrcodelib');
			$qr_code_config['quality'] 		= $this->config->item('quality');
			$qr_code_config['size'] 		= $this->config->item('size');
			$qr_code_config['black'] 		= $this->config->item('black');
			$qr_code_config['white'] 		= $this->config->item('white');

			$this->ci_qr_code->initialize($qr_code_config);

			$image_name = 'qr_code_test.png';

			$params['data'] = 'This QR Code was generated at ' . site_url() . $this->_method_url;
			$params['level'] = ($this->input->post('level')) ? $this->input->post('level') : 'H';
			$params['size'] = ($this->input->post('size')) ? $this->input->post('size') : 10;

			if($this->input->post('display_format') == 'image')
			{
				$params['savename'] = FCPATH.$qr_code_config['imagedir'].$image_name;
				$this->ci_qr_code->generate($params); 
				$this->data['qr_code_image_url'] = base_url().$qr_code_config['imagedir'].$image_name;
				// Display the QR Code here on browser uncomment the below line
				//echo '<img src="'.base_url().$qr_code_config['imagedir'].$image_name.'" />'; 
				$this->load->view('qr_code', $this->data); 
			}
			else
			{
				header("Content-Type: image/png"); 
				$this->ci_qr_code->generate($params);
			} 
		}
	}
}
// END qr_code_generate Controller class
/* End of file qr_code_generate.php */
/* Location: ./application/controllers/qr_code_generate.php */

Views:

Copy the qr_code_generate.php and qr_code.php Views into YOUR_CODEIGNITOR_DIR/application/views

<div class="page-header">
	<h1>
		Generate the QR Code
	</h1>
</div><!-- /.page-header -->
<div class="row">
    <div class="col-xs-12">
        <!-- PAGE CONTENT BEGINS --> 
        <?php echo $form_open; ?> 
    
        
        <div class="row">
            <div class="col-md-6 col-md-offset-3"> 
                <input type="radio" name="display_format" id="display_format_image" value="image" checked="checked">Download as Image
                <input type="radio" name="display_format" id="display_format_print" value="print_dialog">Display Print Dialog
            </div> 
        </div>

        <div class="space-22"></div>

        <div class="row">
            <div class="col-md-6 col-md-offset-3"> 
                <div class="col-md-6"> 
                    Quality of QR Code
                    <select name="level">
                        <option selected="" value="L">L - Smallest</option>
                        <option value="M">M - Medium</option>
                        <option value="Q">Q - Good</option>
                        <option value="H">H - Best</option>
                    </select>
                </div> 
                <div class="col-md-6"> 
                    Size of QR Code
                    <select name="size">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option selected="" value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>
                    </select>
                </div> 
            </div>
        </div> 

        <div class="space-22"></div>


        <div class="row center">
            <div class="col-md-6 col-md-offset-3"> 
                <button class="btn btn-info btn-sm btn-round" type="submit">
                    <i class="ace-icon fa fa-check"></i>
                    Generate Queuevite Card
                </button>

                &nbsp; &nbsp; &nbsp;
                <button class="btn btn-sm btn-round" type="reset">
                    <i class="ace-icon fa fa-undo"></i>
                    Reset
                </button>

                &nbsp; &nbsp; &nbsp; 
                <button class="btn btn-info btn-sm btn-round back" type="button">
                    <i class="ace-icon fa fa-reply"></i>
                    Cancel
                </button> 
                
            </div>
        </div>  

        </form>  
        <!-- PAGE CONTENT ENDS -->
    </div><!-- /.col -->
</div><!-- /.row -->

File to display QR Code

<div class="page-header">
	<h1>
		Generated QR Code
	</h1>
</div><!-- /.page-header -->
<div class="row">
    <div class="col-xs-12">
        <!-- PAGE CONTENT BEGINS --> 
          

        <div class="row">
            <div class="col-md-6 col-md-offset-3"> 
                <img src="<?php echo $qr_code_image_url?>"> 
            </div> 
        </div> 

        <div class="space-22"></div>


        <div class="row center">
            <div class="col-md-6 col-md-offset-3"> 
                <button class="btn btn-info btn-sm btn-round" type="button">
                    <i class="ace-icon fa fa-check"></i>
                    Print Queuevite Card
                </button>

                <button class="btn btn-success btn-sm btn-round" type="button">
                    <i class="ace-icon fa fa-download"></i>
                    Download Queuevite Card
                </button>
                
            </div>
        </div>  

        </form>  
        <!-- PAGE CONTENT ENDS -->
    </div><!-- /.col -->
</div><!-- /.row -->

Download the complete code from attachments and extract on your local.

Third Party Folder:

Copy the qr_code into YOUR_CODEIGNITOR_DIR/application/third_party

tmp Folder

Copy the tmp folder into YOUR_CODEIGNITOR_DIR/

Your feedback and suggestions will be highly appreciated.


Leave a Reply

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