PHP生成条形码 之二 开源之道

该软件支持PHP4和PHP5两个版本,本文中使用的是PHP5的版本。在使用前注意要将PHP的GD模块开启。在Windows中为php_gd2.dll,Linux中为gd.so。将压缩文件解压到Apache,执行http://localhost/barcode/index.php。界面见下图:

1. Type:选择条形码类型

2. Output:输出的图片格式

3. Thickness:条形码高度

4. Resolution:条形码大小

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

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

PHP生成条形码 之二 开源之道

当然,这个程序只是将文字生成为条形码,但使用时不能灵活将其嵌入其他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">
 

效果图:

PHP生成条形码 之二 开源之道

test.php代码:

<?php
// Including all required classes
require('class/BCGFont.php');
require('class/BCGColor.php');
require('class/BCGDrawing.php');

$codebar = $_REQUEST['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']; 
$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);
?>


::源代码下载::





本文转自Gnie博客园博客,原文链接:http://www.cnblogs.com/gnielee/archive/2009/07/29/1533878.html,如需转载请自行联系原作者

上一篇:北大ACM试题分类


下一篇:基于Linux GlassFish v3 配置取代tomcat