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('Location: html/code39.php');
?>

   當直接訪問http://localhost/barcodegen/index.php時,用戶體驗可以體驗該功能,任意選擇項,生成對應的條形碼。需要的話可以將它改版成module來使用。

  

    

  (3)test.php是另外一個例子,通過代碼直接生成HELLO條形碼。 

    當訪問http://localhost/barcodegen/test.php時,HELLO.PNG圖片生成

    

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.條形碼需要的數據內容  

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
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');

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

 

  然后新建test.html文件,向buildcode.php請求數據

View Code
<!DOCTYPE html>
<html>
<head>
<title>Test with embedded image</title>
</head>
<body>
  <img src="buildcode.php?codebar=BCGcode39&text=abc123"/>
</body>
</html>

  最后訪問http://localhost/barcodegen/test.html或訪問http://localhost/barcodegen/buildcode.php?codebar=BCGcode39&text=abc123,瀏覽器直接生成png格式的條形碼  

  

  其中codebar支持的編碼格式可以由用戶請求所得:

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

 

4.驗證

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

  先將圖片保存下來,然后訪問官網提供的驗證功能,將圖片上傳就Ok了! 

    

 

 

作者: ForEvErNoME
出處: http://www.cnblogs.com/ForEvErNoME/
歡迎轉載或分享,但請務必聲明文章出處。如果文章對您有幫助,希望你能 推薦關注
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM