看PHP如何生成的條形碼
前陣子在做一個商家優(yōu)惠券的功能,需要用到條形碼,于是將資料重新整理下。
1.什么是條形碼?
百度百科定義:條形碼(barcode)是將寬度不等的多個黑條和空白,按照一定的編碼規(guī)則排列,用以表達(dá)一組信息的圖形標(biāo)識符。常見的條形碼是由反射率相差很大的黑條(簡稱條)和白條(簡稱空)排成平行線的圖案。在日常生活中,條形碼可以標(biāo)出物品的生產(chǎn)國、制造廠家、商品名稱、生產(chǎn)日期、圖書分類號、郵件地點起止、類別、日期等許多信息。條形碼編碼格式具體請參考
打印出來的優(yōu)惠券,商家需要用驗證器讀取條形碼,來獲得其有效性。
2.如何生成條形碼?
首先找到強(qiáng)大的開源資料,在barcode官網(wǎng)下載barcodegen.1d-php5.v5.0.1.zip版本,然后解壓文件放到你的Apache服務(wù)器的根目錄下
2.1文件結(jié)構(gòu):
2.2具體解析
(1)class文件夾是已封裝好生成條形碼的類,只需要調(diào)用即可。
(2)index.php是一個可選擇條件生成條形碼的功能,是主程序的入口,而html文件夾是提供的被引用的代碼,code39.php指的是指向默認(rèn)的編碼格式。
- <?php
- header('Location: html/code39.php');
- ?>
當(dāng)直接訪問http://localhost/barcodegen/index.php時,用戶體驗可以體驗該功能,任意選擇項,生成對應(yīng)的條形碼。需要的話可以將它改版成module來使用。
(3)test.php是另外一個例子,通過代碼直接生成HELLO條形碼?!?/p>
當(dāng)訪問http://localhost/barcodegen/test.php時,HELLO.PNG圖片生成
- View Code
- <?php
- // 引用class文件夾對應(yīng)的類
- 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'); // 條形碼需要的數(shù)據(jù)內(nèi)容
- } catch(Exception $exception) {
- $drawException = $exception;
- }
- //根據(jù)以上條件繪制條形碼
- $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);
- ?>
#p#
3.實際應(yīng)用
對于上面有個大概的了解后,下面我們可以重新整合下代碼,更加方便的使用它。
首先新建buildcode.php文件中,根據(jù)test.php文件進(jìn)行改寫,從請求的文件中獲取數(shù)據(jù):
1.條形碼的編碼格式
2.條形碼需要的數(shù)據(jù)內(nèi)容
- 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']; //條形碼將要數(shù)據(jù)的內(nèi)容
- // 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();//實例化對應(yīng)的編碼格式
- $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']; //條形碼將要數(shù)據(jù)的內(nèi)容
- $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請求數(shù)據(jù)
- <!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'*/
剩下的就是驗證了
#p#
4.驗證
我們?nèi)绾悟炞C條形碼是否有效,即能否讀出條形碼中的內(nèi)容。
先將圖片保存下來,然后訪問官網(wǎng)提供的驗證功能,將圖片上傳就Ok了!
原文鏈接:http://www.cnblogs.com/ForEvErNoME/archive/2012/04/21/2460944.html
【編輯推薦】