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
2
3
|
<?php
header('location: html/code39.php');
?>
|
(3)test.php是另外一个例子,通过代码直接生成hello条形码。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
view code
<?php
// 引用class文件夹对应的类
require_once('class/bcgfontfile.php');
require_once('class/bcgcolor.php');
require_once('class/bcgdrawing.php');
// 条形码的编码格式
require_once('class/bcgcode39.barcode.php');
// 加载字体大小
$font = new bcgfontfile('./class/font/arial.ttf', 18);
//颜色条形码
$color_black = new bcgcolor(0, 0, 0);
$color_white = new bcgcolor(255, 255, 255);
$drawexception = null;
try {
$code = new bcgcode39();
$code->setscale(2);
$code->setthickness(30); // 条形码的厚度
$code->setforegroundcolor($color_black); // 条形码颜色
$code->setbackgroundcolor($color_white); // 空白间隙颜色
$code->setfont($font); //
$code->parse('hello'); // 条形码需要的数据内容
} catch(exception $exception) {
$drawexception = $exception;
}
//根据以上条件绘制条形码
$drawing = new bcgdrawing('', $color_white);
if($drawexception) {
$drawing->drawexception($drawexception);
} else {
$drawing->setbarcode($code);
$drawing->draw();
}
// 生成png格式的图片
header('content-type: image/png');
$drawing->finish(bcgdrawing::img_format_png);
?>
|
3.实际应用
对于上面有个大概的了解后,下面我们可以重新整合下代码,更加方便的使用它。
首先新建buildcode.php文件中,根据test.php文件进行改写,从请求的文件中获取数据:
1).条形码的编码格式
2).条形码需要的数据内容
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
view code
<?php
// including all required classes
require_once('class/bcgfontfile.php');
require_once('class/bcgcolor.php');
require_once('class/bcgdrawing.php');
$codebar = $_request['codebar']; //条形码将要数据的内容
// including the barcode technology
require_once('class/'.$codebar.'.barcode.php');
// loading font
$font = new bcgfontfile('./class/font/arial.ttf', 12);
// the arguments are r, g, b for color.
$color_black = new bcgcolor(0, 0, 0);
$color_white = new bcgcolor(255, 255, 255);
$drawexception = null;
try {
$code = new $codebar();//实例化对应的编码格式
$code->setscale(2); // resolution
$code->setthickness(23); // 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);
} catch(exception $exception) {
$drawexception = $exception;
}
/* here is the list of the arguments
- filename (empty : display on screen)
- background color */
$drawing = new bcgdrawing('', $color_white);
if($drawexception) {
$drawing->drawexception($drawexception);
} else {
$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);
?>
|
然后新建test.html文件,向buildcode.php请求数据
|
1
2
3
4
5
6
7
8
9
|
<!doctype html>
<html>
<head>
<title>test with embedded image</title>
</head>
<body>
<img src="buildcode.php?codebar=bcgcode39&text=abc123"/>
</body>
</html>
|
最后进行访问,浏览器直接生成png格式的条形码
其中codebar支持的编码格式可以由用户请求所得:
/*'bcgcodabar','bcgcode11','bcgcode39','bcgcode39extended','bcgcode93',
'bcgcode128','bcgean8','bcgean13','bcgisbn','bcgi25','bcgs25','bcgmsi',
'bcgupca','bcgupce','bcgupcext2','bcgupcext5','bcgpostnet','bcgothercode'*/
剩下的就是验证了
4.验证
先将图片保存下来,然后访问官网提供的验证功能,将图片上传就ok了!
今天和大家一起揭秘了php如何生成条形码的,希望大家可以对条形码的形成有个大概的了解,对今后的学习有所帮助。
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 104
-
2025-05-27 87
-
2025-05-25 33
-
2025-05-29 37
-
2025-06-05 71






