fishcracker
fishcracker

Reputation: 2499

codeigniter + zend barcode class

I integrated the Zend_Barcode class by placing Zend directory on application/libraries.

Under my controller:

  public function barcode_gen()
  {
    $this->load->library('Zend/Barcode/Barcode');
    $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
    $rendererOptions = array();
    Zend_Barcode::factory('code39', 'image', $barcodeOptions, $rendererOptions)->render();
  }

However, this results into:

Non-existent class: Barcode

I've also read lots of tutorial but never figured out how to, some tuts are quite outdated.

Even if I followed this thread on CI forums, no luck.

Upvotes: 0

Views: 6963

Answers (4)

clustersnake
clustersnake

Reputation: 1

How about this? The zend barcode library is under the third party folder

 function gen_barcode($product_code = NULL, $bcs = 'code39', $height = 60, $text = 1) {
        $drawText = ($text != 1) ? FALSE : TRUE;
        $this->load->library('zend');
        $this->zend->load('Zend/Barcode');
        $barcodeOptions = array('text' => $product_code, 'barHeight' => $height, 'drawText' => $drawText);
        $rendererOptions = array('imageType' => 'png', 'horizontalPosition' => 'center', 'verticalPosition' => 'middle');
        $imageResource = Zend_Barcode::render($bcs, 'image', $barcodeOptions, $rendererOptions);
        return $imageResource;

    }

Upvotes: 0

peculiar
peculiar

Reputation: 151

I had same problem, and this is what I did to make it works (I'm using CI2 and ZF2). First, integrate CI2 and ZF2, see this example. And include Zend Barcode namespace in my barcode_gen function

function barcode_gen() {
    $this->load->library('Zend');
    $this->zend->load('Zend/Barcode/Barcode');

    $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
    $rendererOptions = array();
    \Zend\Barcode\Barcode::factory('code39', 'image', $barcodeOptions, $rendererOptions)->render();
}

--
UPDATE
I'm using HMVC architecture from this and Loader class is able to load the necessary file from Zend and this is my barcode_gen

function barcode_gen() {
    $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
    $rendererOptions = array();
    \Zend\Barcode\Barcode::factory('code39', 'image', $barcodeOptions, $rendererOptions)->render();
}

Upvotes: 1

Vassilis Barzokas
Vassilis Barzokas

Reputation: 3242

Just to mention that the code of Damien Pirsy ( and also others like here or here ) is for Zend version 1 and not version 2. If you download version 2 then you end up with Fatal error: Class 'Zend_Barcode' not found

I think it will might help others too.

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25435

When you load an external class using $this->load->library() Codeigniter puts the instance of the class in a variable named like the class loaded, so you need to access that way

Since you're then calling it statically instead, I suggested dropping the CI loader method and simply include the class:

public function barcode_gen()
{
   require_once('./application/libraries/Zend/Barcode/Barcode.php');
   //adjust the above path to the correct location
    $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
    $rendererOptions = array();
   Zend_Barcode::factory('code39', 'image', $barcodeOptions, $rendererOptions)->render();
}

Upvotes: 0

Related Questions