PHP5生成条形码的简单实例
时间:2009-09-24
来源:互联网
该软件支持PHP4和PHP5两个版本,本文中使用的是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]
<?php
// Including all required classes require('class/BCGFont.php');
require('class/BCGColor.php');
require('class/BCGDrawing.php');
/*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93', 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi', 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/ $codebar = $_REQUEST['codebar'];
//该软件支持的所有编码,只需调整$codebar参数即可。
// Including the barcode technology include('class/'.$codebar.'.barcode.php');
// Loading Font $font = new BCGFont('./class/font/Arial.ttf', 10);
// The arguments are R, G, B for color. $color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255); $code = new $codebar();
$code->setScale(2); // Resolution $code->setThickness(30);
// Thickness $code->setForegroundColor($color_black); // Color of bars $code->setBackgroundColor($color_white);
// Color of spaces $code->setFont($font); // Font (or 0) $text = $_REQUEST['text'];
//PHP5生成条形码将要数据的内容 $code->parse($text);
/* Here is the list of the arguments 1 - Filename (empty : display on screen) 2 - Background color */
$drawing = new BCGDrawing('', $color_white);
$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);
?> [/php]
在运行过程中出现异常";
throw $e; //重掷异常
}
}
?﹥
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]
<?php
// Including all required classes require('class/BCGFont.php');
require('class/BCGColor.php');
require('class/BCGDrawing.php');
/*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93', 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi', 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/ $codebar = $_REQUEST['codebar'];
//该软件支持的所有编码,只需调整$codebar参数即可。
// Including the barcode technology include('class/'.$codebar.'.barcode.php');
// Loading Font $font = new BCGFont('./class/font/Arial.ttf', 10);
// The arguments are R, G, B for color. $color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255); $code = new $codebar();
$code->setScale(2); // Resolution $code->setThickness(30);
// Thickness $code->setForegroundColor($color_black); // Color of bars $code->setBackgroundColor($color_white);
// Color of spaces $code->setFont($font); // Font (or 0) $text = $_REQUEST['text'];
//PHP5生成条形码将要数据的内容 $code->parse($text);
/* Here is the list of the arguments 1 - Filename (empty : display on screen) 2 - Background color */
$drawing = new BCGDrawing('', $color_white);
$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);
?> [/php]
在运行过程中出现异常";
throw $e; //重掷异常
}
}
?﹥
[ 此帖被0797network在2009-09-24 17:25重新编辑 ]
作者: 0797network 发布时间: 2009-09-24
收下了,谢谢楼主。
作者: oliverzhy 发布时间: 2009-09-24
没有看到附件?
作者: wangfubao 发布时间: 2009-09-24
好东西
作者: xiaojiangdou 发布时间: 2009-09-24
没有附件吗?
作者: whl1295a 发布时间: 2009-09-25
收下了,谢谢楼主。
作者: cnemart 发布时间: 2009-12-07
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28