当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP5教程:PHP的GD模块生成条形码

PHP
连载3:利用PHP创建由Oracle驱动的SOAP服务
连载4:利用PHP创建由Oracle驱动的SOAP服务
理解Zend 框架 用PHP构建完美的阅读器
利用单元测试在每个层上对PHP代码进行检查
视频演示:Zend Platform功能特性详解
967个函式列表 PHP常用语法索引速查表
心得:PHP对文本数据库的五大基本操作方法
日记整理:Apache+MySql+PHP的快速安装
怎样才能成为PHP高手?学会"懒惰"的去编程
使用m17n实现对各国语言间的代码移植和转换
理解Zend 框架(1):构建完美的阅读器
理解Zend 框架(4): 用Zend_HTTP_Client 获取无提要的内容
ajax的最大缺点是什么?对搜索引擎的支持较弱
windows环境下mysql数据库的主从同步备份步骤
用AJAX实现聊天功能(part 1)
实例:用PHP实现Ftp用户的在线管理
利用PHP和CSS改变网页文字大小
PHP开发大型项目的方法[OOP思想]
直接读取数据库信息的三种方法
PHP5.3中新增的魔术常量__DIR__

PHP5教程:PHP的GD模块生成条形码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-04   浏览: 391 ::
收藏到网摘: n/a

本文中使用的是PHP5的版本。在使用前注意要将PHP的GD模块开启。在Windows中为php_gd2.dll,Linux中为gd.so。将压缩文件解压到Apache,执行http://localhost/barcode/index.php。PHP5生成条形码见下图:

1. Type:选择条形码类型

2. Output:输出的图片格式

3. Thickness:条形码高度

4. Resolution:条形码大小

5. Font:条形码下方的文字大小,也可不显示文字

6. Text:条形码打印的内容

当然,这个PHP5生成条形码程序只是将文字生成为条形码,但使用时不能灵活将其嵌入其他PHP程序,我将压缩包里面的test.php做了一些调整,使其能灵活的用于其他程序。运行时只需将条码类型和文字传给test.php即可,例如:

http://localhost/barcode/test.php?codebar=BCGcode39&text=20090729

或运行http://localhost/barcode/mytest.php

mytest.php代码:

<img src="test.php?codebar=BCGcode39&text=20090729">

PHP5生成条形码效果图:

PHP5生成条形码test.php代码:

    <?php  
  1. // Including all required classes require('class/BCGFont.php');  
  2. require('class/BCGColor.php');  require('class/BCGDrawing.php');  
  3. /*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93', 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi', 
  4. 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/ $codebar = $_REQUEST['codebar']; //该软件支持的所有编码,只需调整$codebar参数即可。 
  5. // Including the barcode technology include('class/'.$codebar.'.barcode.php');  
  6. // Loading Font $font = new BCGFont('./class/font/Arial.ttf', 10);  
  7. // The arguments are R, G, B for color. $color_black = new BCGColor(0, 0, 0);  
  8. $color_white = new BCGColor(255, 255, 255);  $code = new $codebar();  
  9. $code->setScale(2); // Resolution $code->setThickness(30); // Thickness 
  10. $code->setForegroundColor($color_black); // Color of bars $code->setBackgroundColor($color_white); // Color of spaces 
  11. $code->setFont($font); // Font (or 0) $text = $_REQUEST['text']; //PHP5生成条形码将要数据的内容 
  12. $code->parse($text);  /* Here is the list of the arguments 
  13. 1 - Filename (empty : display on screen) 2 - Background color */ 
  14. $drawing = new BCGDrawing(''$color_white);  $drawing->setBarcode($code);  
  15. $drawing->draw();  // Header that says it is an image (remove it if you save the barcode to a file) 
  16. header('Content-Type: image/png');  // Draw (or save) the image into PNG format. 
  17. $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);  
  18. ?> 

源码下载