亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

下面給大家介紹thinkphp5 + barcode 生成條形碼,希望對需要的朋友有所幫助!

thinkphp5 + barcode 生成條形碼


603f48cca792f.png


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


603f48fc5c286.png


2、解壓放到“E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,

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


603f493c1e4fb.png


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


生成條形碼的php代碼


<?php
namespace app\index\controller;
use think\Controller;
 
/**
* 條形碼操作類
*/
class Barcode extends Controller
{
    public function createBarcode()
    {
        $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);// 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="{:url('createBarcode')}">


603f49e7c95a7.png


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

html代碼

<img src="{:url('createBarcode',array('text'=>'123'))}">

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]”,上傳剛剛生成的條形碼,如果解析出的參數跟你輸入的一樣,說明條形碼可以用。

603f4a899dbbf.png


分享到:
標簽:thinkphp5 barcode 生成條形碼
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定