简体   繁体   English

codeigniter + zend条码类

[英]codeigniter + zend barcode class

I integrated the Zend_Barcode class by placing Zend directory on application/libraries . 我通过将Zend目录放在application / libraries上来集成Zend_Barcode类。

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. 即使我在CI论坛上关注了这个话题 ,也没有运气。

I had same problem, and this is what I did to make it works (I'm using CI2 and ZF2). 我遇到了同样的问题,这就是我所做的,以使其起作用(我正在使用CI2和ZF2)。 First, integrate CI2 and ZF2, see this example . 首先,集成CI2和ZF2, 请参见本示例 And include Zend Barcode namespace in my barcode_gen function 并在我的barcode_gen函数中包含Zend Barcode名称空间

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 我从这里使用HMVC体系结构,Loader类能够从Zend加载必要的文件,这是我的条形码

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

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 当您使用$ this-> load-> library()加载外部类时,Codeigniter将该类的实例放置在一个名称与所加载类类似的变量中,因此您需要使用这种方式

Since you're then calling it statically instead, I suggested dropping the CI loader method and simply include the class: 由于您随后改为静态调用它,因此我建议删除CI loader方法并仅包含该类:

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();
}

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 只需提及Damien Pirsy(以及其他类似此处此处的代码 )是针对Zend版本1而非版本2的。如果下载版本2,则最终会出现Fatal error: Class 'Zend_Barcode' not found

I think it will might help others too. 我认为这也可能会帮助其他人。

How about this? 这个怎么样? The zend barcode library is under the third party folder zend条码库位于第三方文件夹下

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;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM