0
点赞
收藏
分享

微信扫一扫

PHP生成条形码方法


1、首先找到强大的开源资料,在​​barcode​​​官网下载​​barcodegen.1d-php5.v5.2.1.zip​​版本,然后解压文件放到你的Apache服务器的根目录下

文件结构如下:

PHP生成条形码方法_php

具体解析

(1)class文件夹是已封装好生成条形码的类,只需要调用即可。

(2)index.php是一个可选择条件生成条形码的功能,是主程序的入口,而html文件夹是提供的被引用的代码,code39.php指的是指向默认的编码格式。


  1. <?php  
  2. header('Location: html/code39.php');  
  3. ?> 


当直接访问​​http://localhost/barcodegen/index.php​​时,用户体验可以体验该功能,任意选择项,生成对应的条形码。需要的话可以将它改版成module来使用。


PHP生成条形码方法_html_02

3)test.php是另外一个例子,通过代码直接生成HELLO条形码。 

当访问​​http://localhost/barcodegen/test.php​​时,HELLO.PNG图片生成


PHP生成条形码方法_html_03


生成条形码的代码:

test_1D.php:

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

// Including the barcode technology
require_once('class/BCGcode39.barcode.php');

// Loading Font
$font = new BCGFontFile('./font/Arial.ttf', 18);

// Don't forget to sanitize user inputs
$text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';

// 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 BCGcode39();
$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)
$code->parse($text); // Text
} catch(Exception $exception) {
$drawException = $exception;
}

/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - 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');
header('Content-Disposition: inline; filename="barcode.png"');

// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>

然后新建test_1D.html文件,向test_1D.php请求数据:

<!DOCTYPE html>
<html>
<head>
<title>Test with embedded image</title>
</head>
<body>
<img src="test_1D.php?text=123456" alt="barcode" />
</body>
</html>


最后访问

​​http://localhost/barcodegen/test_1D.html​

或访问 ​​http://localhost/barcodegen/​

test_1D.php?text=123456 ,浏览器直接生成png格式的条形码

PHP生成条形码方法_html_04

其中codebar支持的编码格式可以由用户请求所得:


  1. /*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',   
  2. 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',   
  3. 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/  


剩下的就是验证了

举报

相关推荐

0 条评论