PHP生成条形码barcode

来源:互联网 发布:java微服务框架有哪些 编辑:程序博客网 时间:2024/06/11 13:32

最近在为公司做一个商品库存管理系统,需要用到条形码的相关功能,下面就先介绍如何生成条形码:

1、如何生成条形码?

barcode官网http://www.barcodephp.com下载barcodegen.1d-php5.v5.0.1.zip版本,然后解压文件放到你的Apache服务器的根目录下。

1.1文件结构:


1.2具体解析

(1)class文件夹是已封装好生成条形码的类,只需要调用即可。

(2)index.php是一个可选择条件生成条形码的功能,是主程序的入口,而html文件夹是提供的被引用的代码,code39.php指的是指向默认的编码格式。

<?php
header('Location: html/code39.php');
?>

当直接访问http://localhost/barcodegen/index.php时,用户体验可以体验该功能,任意选择项,生成对应的条形码。需要的话可以将它改版成module来使用。

(3)test.php是另外一个例子,通过代码直接生成HELLO条形码。 

    当访问http://localhost/barcodegen/test.php时,HELLO.PNG图片生成

    

复制代码
<?php// 引用class文件夹对应的类require_once('class/BCGFontFile.php');require_once('class/BCGColor.php');require_once('class/BCGDrawing.php');// 条形码的编码格式require_once('class/BCGcode39.barcode.php');// 加载字体大小$font = new BCGFontFile('./class/font/Arial.ttf', 18);//颜色条形码$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$drawException = null;try {    $code = new BCGcode39();    $code->setScale(2);     $code->setThickness(30); // 条形码的厚度    $code->setForegroundColor($color_black); // 条形码颜色    $code->setBackgroundColor($color_white); // 空白间隙颜色    $code->setFont($font); //     $code->parse('HELLO'); // 条形码需要的数据内容} catch(Exception $exception) {    $drawException = $exception;}//根据以上条件绘制条形码$drawing = new BCGDrawing('', $color_white);if($drawException) {    $drawing->drawException($drawException);} else {    $drawing->setBarcode($code);    $drawing->draw();}// 生成PNG格式的图片header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);?>
然后新建test.html文件,向buildcode.php请求数据

复制代码
<!DOCTYPE html><html><head><title>Test with embedded image</title></head><body>  <img src="buildcode.php?codebar=BCGcode39&text=abc123"/></body></html>
复制代码
最后访问http://localhost/barcodegen/test.html或访问http://localhost/barcodegen/buildcode.php?codebar=BCGcode39&text=abc123,浏览器直接生成png格式的条形码  

  

  其中codebar支持的编码格式可以由用户请求所得:

/*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93', 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi', 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/ 


0 0
原创粉丝点击