Generate barcode in codeigniter using zend library

How to generate barcode in codeigniter? Create display barcode using codeigniter with zend barcode library

In this article we’ll discuss how to generate barcode in codeigniter using the zend barcode library

Download and copy zend files to application/libraries

Controller : Main.php

Create a controller Main.php and paste the below code

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

class Main extends CI_Controller {

	public function index()
	{
		//I'm just using rand() function for data example
		$data=[];
		$code = rand(10000, 99999);

		//load library
		$this->load->library('zend');
		//load in folder Zend
		$this->zend->load('Zend/Barcode');
		//generate barcode
		$imageResource = Zend_Barcode::factory('code128', 'image', array('text'=>$code), array())->draw();
		imagepng($imageResource, 'barcodes/'.$code.'.png');

		$data['barcode'] = 'barcodes/'.$code.'.png';
		$this->load->view('welcome',$data);
	}
	
	
}
  1. Generate a random number
  2. load zend library
  3. Zend_barcode uses a factory method with five arguments which will create an instance of a renderer that extends Zend_Barcode_Renderer_Renderer Abstract. The arguments are:
  • The name of barcode format (e.g., “code39”) (mandatory)
  • The name of renderer (e.g., “image”) (mandatory)
  • Options to pass to the barcode object (an array or Zend_Config object) (optional)
  • Options to pass to the renderer object (an array or Zend_Config object) (optional)
  • Boolean to indicate whether or not to automatically render errors. (optional default TRUE)
  • 4. store the image in barcodes folder
  • load view page with the generated image name

4. imagepng() will Output a PNG image to either the browser or a file

View :

<div>
<img src="<?php echo $barcode; ?>">
</div>

Display the returned image

Hope this tutorial to generate barcode in codeigniter will help you.

Related Posts

send mail in codeigniter

Send mail in CodeIgniter

How to send mail in CodeIgniter using the normal mail() email library. We can send mail in codeigniter using the mail() and SMTP methods. To know how…

smtp mail in codeigniter

Sending SMTP mail in CodeIgniter

How to send SMTP mail in CodeIgniter ? We have normal mail() function in CodeIgniter but in some servers we may have to send emails using SMTP…

generate qr code using codeigniter

Generate QR code using codeigniter

How to generate QR code using codeigniter ? In this tutorial we’ll discuss about generate QR code using codeigniter. QR code is an another form of barcode…

upload image from url

Upload image from URL in codeigniter

How to upload image from url in codeigniter? Rather than selecting a file from drive and uploading into folder we can upload image from URL. For that…

codeigniter pagination

Codeigniter Pagination

How to do codeigniter pagination with database? Codeigniter pagination is a very simple method to show large amount of data with a limit. Displaying large amount of…

Create PDF in Codeigniter using Dompdf – flexible library

How to generate a PDF in Codeigniter using Dompdf ? In this tutorial we will create pdf in codeigniter using Dompdf. Here I used image, inline css,…

Leave a Reply

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