Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP生成条形码实例

PHP生成条形码实例

*文
*文asal
2017-12-27 14:59:113803semak imbas

除了二维码,在商业领域,一维码(条形码)的应用也是非常广泛。因一些旧扫枪设备不能兼容识别二维码,所以有的时候还是会有生成条形码的需求。那么PHP如何来生成条形码呢?本文就来讲解PHP生成条形码的实例。希望对大家有所帮助。

前阵子在接触到一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下,需要的朋友可以参考下

1.什么是条形码?

百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成平行线的图案。在日常生活中,条形码可以标出物品的生产国、制造厂家、商品名称、生产日期、图书分类号、邮件地点起止、类别、日期等许多信息。条形码编码格式具体请参考

打印出来的优惠券,商家需要用验证器读取条形码,来获得其有效性。

2.如何生成条形码?

首先找到强大的开源资料,在barcode官网下载barcodegen.1d-php5.v5.0.1.zip版本,然后解压文件放到你的Apache服务器的根目录下

2.1文件结构:

2.2具体解析

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

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

<?php 
header(&#39;Location: html/code39.php&#39;); 
?>

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

View Code  
 
<?php 
// 引用class文件夹对应的类 
require_once(&#39;class/BCGFontFile.php&#39;); 
require_once(&#39;class/BCGColor.php&#39;); 
require_once(&#39;class/BCGDrawing.php&#39;); 
 
// 条形码的编码格式 
require_once(&#39;class/BCGcode39.barcode.php&#39;); 
 
// 加载字体大小 
$font = new BCGFontFile(&#39;./class/font/Arial.ttf&#39;, 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(&#39;HELLO&#39;); // 条形码需要的数据内容 
} catch(Exception $exception) { 
  $drawException = $exception; 
} 
 
//根据以上条件绘制条形码 
$drawing = new BCGDrawing(&#39;&#39;, $color_white); 
if($drawException) { 
  $drawing->drawException($drawException); 
} else { 
  $drawing->setBarcode($code); 
  $drawing->draw(); 
} 
 
// 生成PNG格式的图片 
header(&#39;Content-Type: image/png&#39;); 
 
 
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG); 
?>

3.实际应用

对于上面有个大概的了解后,下面我们可以重新整合下代码,更加方便的使用它。

首先新建buildcode.php文件中,根据test.php文件进行改写,从请求的文件中获取数据:

1).条形码的编码格式

2).条形码需要的数据内容

View Code  
<?php 
// Including all required classes 
require_once(&#39;class/BCGFontFile.php&#39;); 
require_once(&#39;class/BCGColor.php&#39;); 
require_once(&#39;class/BCGDrawing.php&#39;);  
$codebar = $_REQUEST[&#39;codebar&#39;]; //条形码将要数据的内容  
// Including the barcode technology 
require_once(&#39;class/&#39;.$codebar.&#39;.barcode.php&#39;);  
// Loading Font 
$font = new BCGFontFile(&#39;./class/font/Arial.ttf&#39;, 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[&#39;text&#39;]; //条形码将要数据的内容 
  $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(&#39;&#39;, $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(&#39;Content-Type: image/png&#39;);  
// 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如何生成条形码的,希望大家可以对条形码的形成有个大概的了解,对今后的学习有所帮助。

相关推荐:

php 二维码生成

php 二维码加水印图片 支持ios,android,win8

php 图片操作类,支持生成缩略图,添加水印,上传缩略图

Atas ialah kandungan terperinci PHP生成条形码实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php实现的随机红包算法Artikel seterusnya:PHP概率计算函数