thinkphp5 + barcode 生成條形碼的方法

1、去官網下載類庫 “https://www.barcodebakery.com/en/download”,選擇自己的版本下載

thinkphp5 + barcode 生成條形碼的方法

?推薦教程:thinkphp教程

2、解壓放到“E:phpstudyPHPTutorialWWWguahaovendor下”,其中class文件是所有的類文件,生成條形碼就是調用文件夾里的類,font文件是字體,index.php是一個可選擇條件生成條形碼的功能,是主程序的入口,test_1D.php是給的生成條形碼的例子,test_1D.html是對應的渲染條形碼的頁面

thinkphp5 + barcode 生成條形碼的方法

立即學習PHP免費學習筆記(深入)”;

3、我們可以直接使用官方給的例子(test_1D.php),復制到自己需要用的地方,然后根據自己的需求稍加改動即可,需要注意的是,加載第三方類庫的路徑需要改一下。

生成條形碼的php代碼

<?php namespace appindexcontroller; use thinkController; /** * 條形碼操作類 */ class Barcode extends Controller {     public function createBarcode()     {         $class_dir = VENDOR_PATH.&#39;barcode/class/&#39;;         // Including all required classes         require_once($class_dir.&#39;BCGFontFile.php&#39;);         require_once($class_dir.&#39;BCGColor.php&#39;);         require_once($class_dir.&#39;BCGDrawing.php&#39;);         require_once($class_dir.&#39;BCGcode39.barcode.php&#39;);         // Loading Font         // 注意font和class是同一級文件夾         $font = new BCGFontFile(VENDOR_PATH.&#39;barcode/font/Arial.ttf&#39;, 18);// 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 BCGcode39();             $code->setScale(2);?//?Resolution ????????????$code-&gt;setThickness(30);?//?Thickness ????????????$code-&gt;setForegroundColor($color_black);?//?Color?of?bars ????????????$code-&gt;setBackgroundColor($color_white);?//?Color?of?spaces ????????????$code-&gt;setFont($font);?//?Font?(or?0)??0不顯示文字      ????$text?=?isset($_GET['text'])???$_GET['text']?:?'HELLO'; ????????????$code-&gt;parse($text);?//?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-&gt;drawException($drawException); ????????}?else?{ ????????????$drawing-&gt;setBarcode($code); ????????????$drawing-&gt;draw(); ????????} ????????//?Header?that?says?it?is?an?image?(remove?it?if?you?save?the?barcode?to?a?file) ????????header('Content-Type:?image/png'); ????????header('Content-Disposition:?inline;?filename="barcode.png"'); ????????//?Draw?(or?save)?the?image?into?PNG?format. ????????$drawing-&gt;finish(BCGDrawing::IMG_FORMAT_PNG); ????} ????public?function?barcodedes() ????{ ????????return?$this-&gt;fetch(); ????} } ?&gt;

接受渲染條形碼的Html代碼

@@##@@

?

thinkphp5 + barcode 生成條形碼的方法

當然,src還可以攜帶參數,只需更改以下代碼

html代碼

@@##@@'123'))}"&gt;

php代碼

$text?=?isset($_GET['text'])???$_GET['text']?:?'HELLO';

改成

$text?=?input('text');??????//接收的參數

4、如果想把條形碼保存到本地,在實例化“BCGDrawing”的時候填寫保存路徑即可

//?文件路徑 ????????$file_dir?=?'uploads/barcode/'.date('Y-m-d'); ????????if?(!file_exists($file_dir))?{ ????????????mkdir($file_dir,0755,true); ????????} ????????$imgUrl?=?$file_dir.'/'.time().'.png'; ????????$class_dir?=?VENDOR_PATH.'barcode/class/'; ????????//?Including?all?required?classes ????????require_once($class_dir.'BCGFontFile.php'); ????????require_once($class_dir.'BCGColor.php'); ????????require_once($class_dir.'BCGDrawing.php'); ????????require_once($class_dir.'BCGcode39.barcode.php'); ????????//?Loading?Font ????????//?注意font和class是同一級文件夾 ????????$font?=?new?BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf',?18); ????????//?Don't?forget?to?sanitize?user?inputs ????????//?$text?=?isset($_GET['text'])???$_GET['text']?:?'HELLO'; ????????//?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?BCGcode39(); ????????????$code-&gt;setScale(2);?//?Resolution ????????????$code-&gt;setThickness(30);?//?Thickness ????????????$code-&gt;setForegroundColor($color_black);?//?Color?of?bars ????????????$code-&gt;setBackgroundColor($color_white);?//?Color?of?spaces ????????????$code-&gt;setFont($font);?//?Font?(or?0) ????????????$text?=?input('text');??????//接收的參數 ????????????$text?=?isset($text)???$text?:'無參數';?????? ????????????$code-&gt;parse($text);?//?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($imgUrl,?$color_white); ????????if($drawException)?{ ????????????$drawing-&gt;drawException($drawException); ????????}?else?{ ????????????$drawing-&gt;setBarcode($code); ????????????$drawing-&gt;draw(); ????????} ????????$drawing-&gt;finish(BCGDrawing::IMG_FORMAT_PNG);

?

5、生成條形碼之后,怎么判定條形碼是否能用呢?可以把條形碼保存成圖片到本地,打開官網“https://www.onlinebarcodereader.com/”,上傳剛剛生成的條形碼,如果解析出的參數跟你輸入的一樣,說明條形碼可以用。

?thinkphp5 + barcode 生成條形碼的方法

thinkphp5 + barcode 生成條形碼的方法thinkphp5 + barcode 生成條形碼的方法

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享