thinkphp5 + barcode 生成條形碼

image

一、去官網下載類庫 「[https://www.barcodebakery.com...]」,選擇本身的版本下載php

image

二、解壓放到「E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下」,其中class文件是全部的類文件,生成條形碼就是調用文件夾裏的類,font文件是字體,index.php是一個可選擇條件生成條形碼的功能,是主程序的入口,test_1D.php是給的生成條形碼的例子,test_1D.html是對應的渲染條形碼的頁面html

image

三、咱們能夠直接使用官方給的例子(test_1D.php),複製到本身須要用的地方,而後根據本身的需求稍加改動便可,須要注意的是,加載第三方類庫的路徑須要改一下。app

生成條形碼的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代碼fetch

<img src="{:url('createBarcode')}">

image

固然,src還能夠攜帶參數,只需更改如下代碼ui

html代碼this

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

php代碼把url

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

改爲spa

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

四、若是想把條形碼保存到本地,在實例化「BCGDrawing」的時候_填寫保存路徑便可_3d

// 文件路徑
        $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);

五、生成條形碼以後,怎麼斷定條形碼是否能用呢?

能夠把條形碼保存成圖片到本地,打開官網「[https://www.onlinebarcoderead...]」,上傳剛剛生成的條形碼,若是解析出的參數跟你輸入的同樣,說明條形碼能夠用。

image

image

相關文章
相關標籤/搜索