给朋友做的一个项目,其中用到生成条形码,主要用到了http://www.barcodephp.com/这个开源的类库,把它集成到了CI里面,代码和集成方法稍后给出来
1.到http://www.barcodephp.com/en/download下载包
2.解压后,将class文件夹放到application/libraries下面,可以改名为barcode,如果需要用他字体的话,font也放到你放字体的地方
3.application/libraries下面新建Barcode.php
<?php include 'barcode/BCGFontFile.php'; include 'barcode/BCGColor.php'; include 'barcode/BCGDrawing.php'; include 'barcode/BCGcode39.barcode.php'; class Barcode { public $colorFront; public $colorBlack; public $font; function __construct(){ $this->colorBlack = new BCGColor(255,255,255); $this->colorFront = new BCGColor(0,0,0); // $this->font = new BCGFontFile('/assets/font/Arial.ttf', 18);//需要字体在这里指定 } public function genBarcode($text) { $code = new BCGcode39(); $code->setScale(2); // Resolution $code->setThickness(30); // Thickness $code->setForegroundColor($this->colorFront); // Color of bars $code->setBackgroundColor($this->colorBlack); // Color of spaces // $code->setFont($this->font); // Font (or 0) $code->parse($text); // Text $drawing = new BCGDrawing('', $this->colorBlack); $drawing->setBarcode($code); $drawing->draw(); header('Content-Type: image/png'); $drawing->finish((BCGDrawing::IMG_FORMAT_PNG)); } }
调用的时候,直接load这个barcode,然后调用genBarcode即可