首頁 php框架 ThinkPHP 圖文詳解thinkphp5+barcode產生條碼

圖文詳解thinkphp5+barcode產生條碼

Mar 03, 2021 pm 03:47 PM
thinkphp5

下面由thinkphp教學專欄為大家介紹thinkphp5 barcode 產生條碼,希望對需要的朋友有幫助!

thinkphp5 barcode 產生條碼

圖文詳解thinkphp5+barcode產生條碼

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

圖文詳解thinkphp5+barcode產生條碼

2、解壓縮放到“E :\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,其中class檔案是所有的類別檔案,產生條碼就是呼叫資料夾裡的類別,font檔案是字體,index.php是一個可選擇條件產生條碼的功能,是主程式的入口,test_1D.php是給的生成條碼的例子,test_1D.html是對應的渲染條碼的頁面

圖文詳解thinkphp5+barcode產生條碼

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

產生條碼的php程式碼

<?php namespace app\index\controller;
use think\Controller;

/**
* 条形码操作类
*/
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->setThickness(30); // Thickness
            $code->setForegroundColor($color_black); // Color of bars
            $code->setBackgroundColor($color_white); // Color of spaces
            $code->setFont($font); // Font (or 0)  0不显示文字
         $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
            $code->parse($text); // 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');
        header('Content-Disposition: inline; filename="barcode.png"');

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

    }

    public function barcodedes()
    {
        return $this->fetch();
    }
}
?>
登入後複製

接受渲染條碼的Html程式碼

<img  src="/static/imghw/default1.png" data-src="{:url('createBarcode')}" class="lazy" alt="圖文詳解thinkphp5+barcode產生條碼" >
登入後複製

圖文詳解thinkphp5+barcode產生條碼

##當然,src還可以攜帶參數,只要更改以下程式碼

html程式碼

<img  src="/static/imghw/default1.png" data-src="{:url('createBarcode',array('text'=>'123'))}" class="lazy" alt="圖文詳解thinkphp5+barcode產生條碼" >
登入後複製

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->setScale(2); // Resolution
            $code->setThickness(30); // Thickness
            $code->setForegroundColor($color_black); // Color of bars
            $code->setBackgroundColor($color_white); // Color of spaces
            $code->setFont($font); // Font (or 0)
            $text = input('text');      //接收的参数
            $text = isset($text) ? $text :'无参数';      
            $code->parse($text); // Text
        } catch(Exception $exception) {
            $drawException = $exception;
        }

        /* Here is the list of the arguments
- Filename (empty : display on screen)
- Background color */
        // 保存到本地 (路径,颜色)路径为空则表示显示到页面上
        $drawing = new \BCGDrawing($imgUrl, $color_white);
        if($drawException) {
            $drawing->drawException($drawException);
        } else {
            $drawing->setBarcode($code);
            $drawing->draw();
        }
        $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
登入後複製
5、產生條碼之後,怎麼判定條碼是否能用呢?

可以把條碼保存成圖片到本地,打開官網“[https://www.barcodebakery.com/en/download]”,上傳剛剛生成的條碼,如果解析出的參數跟你輸入的一樣,說明條碼可以用。

圖文詳解thinkphp5+barcode產生條碼

相關推薦:

最新的10個thinkphp影片教學

以上是圖文詳解thinkphp5+barcode產生條碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

寶塔部署thinkphp5報錯怎麼辦 寶塔部署thinkphp5報錯怎麼辦 Dec 19, 2022 am 11:04 AM

寶塔部署thinkphp5報錯的解決方法:1、開啟寶塔伺服器,安裝php pathinfo擴充並啟用;2、設定「.access」文件,內容為「RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]」;3、在網站管理裡面,啟用thinkphp的偽靜態即可。

thinkphp5 url重寫不行怎麼辦 thinkphp5 url重寫不行怎麼辦 Dec 12, 2022 am 09:31 AM

thinkphp5 url重寫不行的解決方法:1、查看httpd.conf設定檔中是否載入了mod_rewrite.so模組;2、將AllowOverride None中的None改為All;3、修改Apache設定檔.htaccess為「RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]」保存即可。

thinkphp5怎麼取得請求過來的網址 thinkphp5怎麼取得請求過來的網址 Dec 20, 2022 am 09:48 AM

thinkphp5取得請求網址的方法:1.使用「\think\Request」類別的「$request = Request::instance();」方法取得目前的url資訊;2、透過自帶的助手函數「$request-> url()」取得包含網域的完整URL位址。

thinkphp5 post不值怎麼辦 thinkphp5 post不值怎麼辦 Dec 06, 2022 am 09:29 AM

thinkphp5 post無法得到值是因為TP5是透過strpos函數在Header的content-type值中找出app/json字串的,其解決辦法就是設定Header的content-type值為app/json即可。

怎麼去除thinkphp5標題欄icon 怎麼去除thinkphp5標題欄icon Dec 20, 2022 am 09:24 AM

移除thinkphp5標題列icon的方法:1、找到thinkphp5框架public下的favicon.ico檔案;2、刪除該檔案或選擇另一張圖片命名改為favicon.ico,並取代原favicon.ico檔案即可。

thinkphp5提示控制器不存在怎麼辦 thinkphp5提示控制器不存在怎麼辦 Dec 06, 2022 am 10:43 AM

thinkphp5提示控制器不存在的解決方法:1、檢查對應的控制器裡面的命名空間是否寫對,修改為正確的命名空間;2、打開對應的tp文件,修改類別名稱即可。

ThinkPHP5怎麼查詢昨天的數據 ThinkPHP5怎麼查詢昨天的數據 Dec 05, 2022 am 09:20 AM

ThinkPHP5查詢昨天資料的方法:1、開啟ThinkPHP5相關檔案;2、透過表達式「db('table')->whereTime('c_time', 'yesterday')->select();」查詢昨天的資料即可。

thinkphp5報錯提示怎麼設定 thinkphp5報錯提示怎麼設定 Dec 07, 2022 am 10:31 AM

thinkphp5設定報錯提示的方法:1、進入專案根目錄下的public資料夾,開啟index.php入口檔案;2、檢視偵錯模式開關的註解;3、將「APP_DEBUG」常數的值調整為true即可展示錯誤訊息提示。

See all articles