
2.2具体解析
(1)class文件夹是已封装好生成条形码的类,只需要调用即可。
(2)index.php是一个可选择条件生成条形码的功能,是主程序的入口,而html文件夹是提供的被引用的代码,code39.php指的是指向默认的编码格式。
<?php header("Location: html/code39.php"); ?>

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

View Code <?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); ?>3.实际应用View Code<?php // Including all required classes require_once("class/BCGFontFile.php"); require_once("class/BCGColor.php"); require_once("class/BCGDrawing.php");$codebar = $_REQUEST["codebar"]; //条形码将要数据的内容// Including the barcode technology require_once("class/".$codebar.".barcode.php");// Loading Font $font = new BCGFontFile("./class/font/Arial.ttf", 12);// The arguments are R, G, B for color. $color_black = new BCGColor(0, 0, 0); $color_white = new BCGColor(255, 255, 255);$drawException = null; try { $code = new $codebar();//实例化对应的编码格式 $code->setScale(2); // Resolution $code->setThickness(23); // Thickness $code->setForegroundColor($color_black); // Color of bars $code->setBackgroundColor($color_white); // Color of spaces $code->setFont($font); // Font (or 0) $text = $_REQUEST["text"]; //条形码将要数据的内容 $code->parse($text); } catch(Exception $exception) { $drawException = $exception; }/* Here is the list of the arguments- Filename (empty : display on screen)- Background color */ $drawing = new BCGDrawing("", $color_white); if($drawException) { $drawing->drawException($drawException); } else { $drawing->setBarcode($code); $drawing->draw(); }// Header that says it is an image (remove it if you save the barcode to a file) header("Content-Type: image/png");// Draw (or save) the image into PNG format. $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>最后进行访问,浏览器直接生成png格式的条形码

其中codebar支持的编码格式可以由用户请求所得:
/*"BCGcodabar","BCGcode11","BCGcode39","BCGcode39extended","BCGcode93",
"BCGcode128","BCGean8","BCGean13","BCGisbn","BCGi25","BCGs25","BCGmsi",
"BCGupca","BCGupce","BCGupcext2","BCGupcext5","BCGpostnet","BCGothercode"*/
剩下的就是验证了
4.验证
我们如何验证条形码是否有效,即能否读出条形码中的内容。
先将图片保存下来,然后访问官网提供的验证功能,将图片上传就Ok了!

今天和大家一起揭秘了php如何生成条形码的,希望大家可以对条形码的形成有个大概的了解,对今后的学习有所帮助。