前陣子在做一個商家優惠券的功能,需要用到條碼,於是將資料重新整理下。
1.什麼是條碼?
百度百科定義:條碼(barcode)是將寬度不等的多個黑條和空白,按照一定的編碼規則排列,用以表達一組資訊的圖形標識符。常見的條碼是由反射率相差很大的黑條(簡稱條)和白條(簡稱空)排成平行線的圖案。在日常生活中,條碼可以標出物品的生產國、製造廠家、商品名稱、生產日期、圖書分類號、郵件地點起止、類別、日期等許多資訊。條碼編碼格式具體請參考
列印出來的優惠券,商家需要用驗證器讀取條碼,來獲得其有效性。
2.如何產生條碼?
首先找到強大的開源資料,在barcode官網下載barcodegen.1d-php5.v5.0.1.zip版本,然後解壓檔案放到你的Apache伺服器的根目錄下。
2.1檔案結構:
2.2具體解析
(1)class檔案夾是已封裝好產生條碼的類,只需要調用即可。
(2)index.php是一個可選擇條件產生條碼的功能,是主程式的入口,而html檔案夾是提供的被引用的代碼,code39.php指的是指向預設的編碼格式。
<?phpheader('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 classesrequire_once('class/BCGFontFile.php');require_once('class/BCGColor.php');require_once('class/BCGDrawing.php');$codebar = $_REQUEST['codebar']; //條碼將要資料的內容// Including the barcode technologyrequire_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 arguments1 - 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/
歡迎轉載或分享,但請務必聲明文章出處。如果文章對您有協助,希望你能 推薦 或 關注