search
HomeBackend DevelopmentPHP TutorialPHP生成条形码大揭秘_PHP

1.什么是条形码?

百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成平行线的图案。在日常生活中,条形码可以标出物品的生产国、制造厂家、商品名称、生产日期、图书分类号、邮件地点起止、类别、日期等许多信息。条形码编码格式具体请参考

打印出来的优惠券,商家需要用验证器读取条形码,来获得其有效性。

2.如何生成条形码?

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

2.1文件结构:

2.2具体解析

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

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

<&#63;php 
header('Location: html/code39.php'); 
&#63;> 

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

View Code  
 
<&#63;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); 
&#63;>

3.实际应用

对于上面有个大概的了解后,下面我们可以重新整合下代码,更加方便的使用它。

首先新建buildcode.php文件中,根据test.php文件进行改写,从请求的文件中获取数据:

1).条形码的编码格式

2).条形码需要的数据内容

View Code  
<&#63;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); 
&#63;> 

然后新建test.html文件,向buildcode.php请求数据

<!DOCTYPE html> 
<html> 
<head> 
<title>Test with embedded image</title> 
</head> 
<body> 
 <img  src="/static/imghwm/default1.png"  data-src="buildcode.php&#63;codebar=BCGcode39&text=abc123"  class="lazy"/ alt="PHP生成条形码大揭秘_PHP" > 
</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如何生成条形码的,希望大家可以对条形码的形成有个大概的了解,对今后的学习有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

HTTP Method Verification in LaravelHTTP Method Verification in LaravelMar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::downloadDiscover File Downloads in Laravel with Storage::downloadMar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use