PHP5生成条形码的简单实例

开发 后端
本PHP5生成条形码的简单实例中使用的是PHP5的版本。在使用前注意要将PHP的GD模块开启。在Windows中为php_gd2.dll,Linux中为gd.so。将压缩文件解压到Apache。

该软件支持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代码:

  1. <img src="test.php?codebar=BCGcode39&text=20090729"> 
PHP5生成条形码效果图:

运行实例效果图

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

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

【编辑推荐】

  1. PHP 5中新增加日期(date)函数的常量简介
  2. 在PHP中如何判断AJAX请求
  3. 如何书写安全的PHP代码
  4. PHP5中的异常处理详解
  5. 实战PHP5+Apache2.2+MySQL5.1配置经历
责任编辑:彭凡 来源: cnblogs
相关推荐

2012-06-14 13:09:50

PHP

2023-08-15 19:24:37

Python条形码

2011-07-07 16:53:56

iOS 条形码扫描

2011-12-27 20:33:40

移动应用

2015-10-28 13:33:50

二维码条形码扫描源码

2012-03-19 09:16:02

JavajZebra

2009-11-24 17:01:39

PHP5多重继承

2009-10-19 13:34:18

VB.NET条形码编程

2012-05-25 10:26:18

JavaAspose.BarC

2015-07-22 09:15:37

物联网条形码

2009-12-03 13:50:16

PHP5异常处理

2009-11-23 20:00:25

PHP5接口PHP5抽象类

2016-03-09 10:03:25

RFID技术条形码二维码

2009-03-16 16:08:09

PHP异常

2011-03-11 14:02:55

LAMP安装PHP5

2009-11-24 16:28:41

PHP5魔术函数

2009-11-18 18:33:23

Linux PHP5安

2021-03-26 16:10:18

鸿蒙HarmonyOS应用

2019-03-08 08:55:16

PHP7PHP5web安全

2009-12-11 17:33:56

PHP5常用函数
点赞
收藏

51CTO技术栈公众号