首頁  >  文章  >  後端開發  >  PHP條碼的定義及產生方法

PHP條碼的定義及產生方法

墨辰丷
墨辰丷原創
2018-06-07 11:40:424512瀏覽

本篇主要介紹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指的是指向預設的編碼格式。

<?php 
header(&#39;Location: html/code39.php&#39;); 
?>

(3)test.php是另一個例子,透過程式碼直接產生HELLO條碼。

View Code  
 
<?php 
// 引用class文件夹对应的类 
require_once(&#39;class/BCGFontFile.php&#39;); 
require_once(&#39;class/BCGColor.php&#39;); 
require_once(&#39;class/BCGDrawing.php&#39;); 
 
// 条形码的编码格式 
require_once(&#39;class/BCGcode39.barcode.php&#39;); 
 
// 加载字体大小 
$font = new BCGFontFile(&#39;./class/font/Arial.ttf&#39;, 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(&#39;HELLO&#39;); // 条形码需要的数据内容 
} catch(Exception $exception) { 
  $drawException = $exception; 
} 
 
//根据以上条件绘制条形码 
$drawing = new BCGDrawing(&#39;&#39;, $color_white); 
if($drawException) { 
  $drawing->drawException($drawException); 
} else { 
  $drawing->setBarcode($code); 
  $drawing->draw(); 
} 
 
// 生成PNG格式的图片 
header(&#39;Content-Type: image/png&#39;); 
 
 
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG); 
?>

3.實際應用

對於上面有個大概的了解後,下面我們可以重新整合下程式碼,更方便的使用它。

首先新建buildcode.php檔案中,依照test.php檔案進行改寫,從要求的檔案取得資料:

1).條碼的編碼格式

2).條碼所需的資料內容

View Code  
<?php 
// Including all required classes 
require_once(&#39;class/BCGFontFile.php&#39;); 
require_once(&#39;class/BCGColor.php&#39;); 
require_once(&#39;class/BCGDrawing.php&#39;);  
$codebar = $_REQUEST[&#39;codebar&#39;]; //条形码将要数据的内容  
// Including the barcode technology 
require_once(&#39;class/&#39;.$codebar.&#39;.barcode.php&#39;);  
// Loading Font 
$font = new BCGFontFile(&#39;./class/font/Arial.ttf&#39;, 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[&#39;text&#39;]; //条形码将要数据的内容 
  $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(&#39;&#39;, $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(&#39;Content-Type: image/png&#39;);  
// Draw (or save) the image into PNG format. 
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG); 
?>

然後新建test.html文件,向buildcode.php請求資料

<!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','BCGd 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/ 
剩下的就是驗證了

##4#4 .驗證

我們如何驗證條碼是否有效,即能否讀出條碼中的內容。

先將圖片儲存下來,然後存取官網提供的驗證功能,將圖片上傳就Ok了! 

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP實作一個多功能購物網站

##php之可變函數

PHP實作模擬登陸功能#

以上是PHP條碼的定義及產生方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn