Search code examples
phpzend-framework2codeigniter-2barcode

CodeIgniter 2 + Zend 2 library barcode


Problem: rendering barcodes in CodeIgniter via Zend library barcode.

I googled, and also tried all tutorials on first 2 pages. I stackoverflowed and found quiet a few topics on my problem, even few are marked as answered but no luck.

Finally I tried this https://stackoverflow.com/a/15480779/1564365 but yet another error message.

error:

Fatal error: Class 'Zend\Barcode\ObjectPluginManager' not found

that means it is actually loading Barcode library but with error.

sidenote: ZF 2.2 fresh download (today), CI 2.1.3 fresh download (today)


Solution

  • To solve this, I am forced to use ZF1. step by step:

    1. Download (Zend Framework 1.12.3 Full) from here
    2. Unzip files and locate folder Zend in ./libraries folder copy it to CI application/libraries
    3. Create new file inside (CI) application/libraries/Zend.php "loader for ZF"

    with code as follows

    <?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
    
    /**
     * Zend Framework Loader
     *
     * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
     * in CI installation's 'application/libraries' folder
     * You can put it elsewhere but remember to alter the script accordingly
     *
     * Usage:
     *   1) $this->load->library('zend', 'Zend/Package/Name');
     *   or
     *   2) $this->load->library('zend');
     *      then $this->zend->load('Zend/Package/Name');
     *
     * * the second usage is useful for autoloading the Zend Framework library
     * * Zend/Package/Name does not need the '.php' at the end
     */
    class CI_Zend
    {
        /**
         * Constructor
         *
         * @param   string $class class name
         */
        function __construct($class = NULL)
        {
            // include path for Zend Framework
            // alter it accordingly if you have put the 'Zend' folder elsewhere
            ini_set('include_path',
            ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
    
            if ($class)
            {
                require_once (string) $class . EXT;
                log_message('debug', "Zend Class $class Loaded");
            }
            else
            {
                log_message('debug', "Zend Class Initialized");
            }
        }
    
        /**
         * Zend Class Loader
         *
         * @param   string $class class name
         */
        function load($class)
        {
            require_once (string) $class . EXT;
            log_message('debug', "Zend Class $class Loaded");
        }
    }
    

    and controllers method should be as follows

    function barcode() {
        $this->load->library('zend');
        $this->zend->load('Zend/Barcode');
        $test = Zend_Barcode::draw('ean8', 'image', array('text' => '1234565'), array());
        var_dump($test);
        imagejpeg($test, 'barcode.jpg', 100);
    }