看PHP如何生成的条形码

前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下。

10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有新兴免费网站建设让你可以放心的选择与我们合作。

1.什么是条形码?

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

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

2.如何生成条形码?

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

2.1文件结构:

2.2具体解析

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

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

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

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

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

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

 
 
 
 
  1. View Code   
  2.  
  3. // 引用class文件夹对应的类  
  4. require_once('class/BCGFontFile.php');  
  5. require_once('class/BCGColor.php');  
  6. require_once('class/BCGDrawing.php');  
  7.  
  8. // 条形码的编码格式  
  9. require_once('class/BCGcode39.barcode.php');  
  10.  
  11. // 加载字体大小  
  12. $font = new BCGFontFile('./class/font/Arial.ttf', 18);  
  13.  
  14. //颜色条形码  
  15. $color_black = new BCGColor(0, 0, 0);  
  16. $color_white = new BCGColor(255, 255, 255);  
  17.  
  18. $drawException = null;  
  19. try {  
  20.     $code = new BCGcode39();  
  21.     $code->setScale(2);   
  22.     $code->setThickness(30); // 条形码的厚度  
  23.     $code->setForegroundColor($color_black); // 条形码颜色  
  24.     $code->setBackgroundColor($color_white); // 空白间隙颜色  
  25.     $code->setFont($font); //   
  26.     $code->parse('HELLO'); // 条形码需要的数据内容  
  27. } catch(Exception $exception) {  
  28.     $drawException = $exception;  
  29. }  
  30.  
  31. //根据以上条件绘制条形码  
  32. $drawing = new BCGDrawing('', $color_white);  
  33. if($drawException) {  
  34.     $drawing->drawException($drawException);  
  35. } else {  
  36.     $drawing->setBarcode($code);  
  37.     $drawing->draw();  
  38. }  
  39.  
  40. // 生成PNG格式的图片  
  41. header('Content-Type: image/png');  
  42.  
  43.  
  44. $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);  
  45. ?> 

#p#

3.实际应用

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

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

1.条形码的编码格式

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

 
 
 
 
  1. View Code    
  2. // Including all required classes  
  3. require_once('class/BCGFontFile.php');  
  4. require_once('class/BCGColor.php');  
  5. require_once('class/BCGDrawing.php');   
  6. $codebar = $_REQUEST['codebar']; //条形码将要数据的内容   
  7. // Including the barcode technology  
  8. require_once('class/'.$codebar.'.barcode.php');   
  9. // Loading Font  
  10. $font = new BCGFontFile('./class/font/Arial.ttf', 12);   
  11. // The arguments are R, G, B for color.  
  12. $color_black = new BCGColor(0, 0, 0);  
  13. $color_white = new BCGColor(255, 255, 255);   
  14. $drawException = null;  
  15. try {  
  16.     $code = new $codebar();//实例化对应的编码格式  
  17.     $code->setScale(2); // Resolution  
  18.     $code->setThickness(23); // Thickness  
  19.     $code->setForegroundColor($color_black); // Color of bars  
  20.     $code->setBackgroundColor($color_white); // Color of spaces  
  21.     $code->setFont($font); // Font (or 0)  
  22.     $text = $_REQUEST['text']; //条形码将要数据的内容  
  23.     $code->parse($text);  
  24. } catch(Exception $exception) {  
  25.     $drawException = $exception;  
  26. }   
  27. /* Here is the list of the arguments  
  28.  - Filename (empty : display on screen)  
  29.  - Background color */ 
  30. $drawing = new BCGDrawing('', $color_white);  
  31. if($drawException) {  
  32.     $drawing->drawException($drawException);  
  33. } else {  
  34.     $drawing->setBarcode($code);  
  35.     $drawing->draw();  
  36. }   
  37. // Header that says it is an image (remove it if you save the barcode to a file)  
  38. header('Content-Type: image/png');   
  39. // Draw (or save) the image into PNG format.  
  40. $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);  
  41. ?> 

然后新建test.html文件,向buildcode.php请求数据

 
 
 
 
  1.  
  2.  
  3.  
  4. Test with embedded image 
  5.  
  6.  
  7.    
  8.  
  9.  

***访问http://localhost/barcodegen/test.html或访问http://localhost/barcodegen/buildcode.php?codebar=BCGcode39&text=abc123,浏览器直接生成png格式的条形码  

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

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

剩下的就是验证了

#p#

4.验证

我们如何验证条形码是否有效,即能否读出条形码中的内容。

先将图片保存下来,然后访问官网提供的验证功能,将图片上传就Ok了! 

原文链接:http://www.cnblogs.com/ForEvErNoME/archive/2012/04/21/2460944.html

【编辑推荐】

  1. PHP使用Google Plus Oauth登录
  2. PHP实现Amazon简单邮件服务SMTP
  3. PHP实现人人OAuth登录和API调用
  4. 图文并茂PHP跟老大的对话
  5. PHP生成GIF动态图片验证码

分享标题:看PHP如何生成的条形码
网址分享:http://www.hantingmc.com/qtweb/news43/293693.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联