您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“PHP生成條形碼的方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
1.什么是條形碼?
百度百科定義:條形碼(barcode)是將寬度不等的多個黑條和空白,按照一定的編碼規(guī)則排列,用以表達一組信息的圖形標識符。常見的條形碼是由反射率相差很大的黑條(簡稱條)和白條(簡稱空)排成平行線的圖案。在日常生活中,條形碼可以標出物品的生產(chǎn)國、制造廠家、商品名稱、生產(chǎn)日期、圖書分類號、郵件地點起止、類別、日期等許多信息。條形碼編碼格式具體請參考
打印出來的優(yōu)惠券,商家需要用驗證器讀取條形碼,來獲得其有效性。
2.如何生成條形碼?
首先找到強大的開源資料,在barcode官網(wǎng)下載barcodegen.1d-php5.v5.0.1.zip版本,然后解壓文件放到你的Apache服務器的根目錄下
2.1文件結(jié)構(gòu):
2.2具體解析
(1)class文件夾是已封裝好生成條形碼的類,只需要調(diào)用即可。
(2)index.php是一個可選擇條件生成條形碼的功能,是主程序的入口,而html文件夾是提供的被引用的代碼,code39.php指的是指向默認的編碼格式。
<?php header('Location: html/code39.php'); ?>
(3)test.php是另外一個例子,通過代碼直接生成HELLO條形碼?!?/p>
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'); // 條形碼需要的數(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); ?>
3.實際應用
對于上面有個大概的了解后,下面我們可以重新整合下代碼,更加方便的使用它。
首先新建buildcode.php文件中,根據(jù)test.php文件進行改寫,從請求的文件中獲取數(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();//實例化對應的編碼格式 $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>
最后進行訪問,瀏覽器直接生成png格式的條形碼
其中codebar支持的編碼格式可以由用戶請求所得:
/*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',
'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',
'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/
剩下的就是驗證了
4.驗證
我們?nèi)绾悟炞C條形碼是否有效,即能否讀出條形碼中的內(nèi)容。
先將圖片保存下來,然后訪問官網(wǎng)提供的驗證功能,將圖片上傳就Ok了!
“PHP生成條形碼的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。