首頁  >  文章  >  php框架  >  thinkphp5 + barcode 產生條碼的方法

thinkphp5 + barcode 產生條碼的方法

藏色散人
藏色散人轉載
2020-04-12 14:47:182682瀏覽

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

thinkphp5 + barcode 產生條碼的方法

 推薦教學:thinkphp教學

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[&#39;text&#39;]) ? $_GET[&#39;text&#39;] : &#39;HELLO&#39;;
            $code->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(&#39;&#39;, $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(&#39;Content-Type: image/png&#39;);
        header(&#39;Content-Disposition: inline; filename="barcode.png"&#39;);
        // Draw (or save) the image into PNG format.
        $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
    }
    public function barcodedes()
    {
        return $this->fetch();
    }
}
?>

接受渲染條碼的Html程式碼

<img  src="{:url(&#39;createBarcode&#39;)}" alt="thinkphp5 + barcode 產生條碼的方法" >

 

thinkphp5 + barcode 產生條碼的方法

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

html程式碼

<img  src="{:url(&#39;createBarcode&#39;,array(&#39;text&#39;= alt="thinkphp5 + barcode 產生條碼的方法" >&#39;123&#39;))}">

php程式碼

#把

$text = isset($_GET[&#39;text&#39;]) ? $_GET[&#39;text&#39;] : &#39;HELLO&#39;;

改成

$text = input(&#39;text&#39;);      //接收的参数

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[&#39;text&#39;]) ? $_GET[&#39;text&#39;] : &#39;HELLO&#39;;
        // 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(&#39;text&#39;);      //接收的参数
            $text = isset($text) ? $text :'无参数';      
            $code->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->drawException($drawException);
        } else {
            $drawing->setBarcode($code);
            $drawing->draw();
        }
        $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);

 

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

 thinkphp5 + barcode 產生條碼的方法

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

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除