thinkphp5 + barcode 生成條形碼

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

 

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

三、咱們能夠直接使用官方給的例子(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)
     
$text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
$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('', $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')}">

 

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

html代碼this

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

php代碼url

spa

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

改爲code

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

四、生成條形碼以後,怎麼斷定條形碼是否能用呢?能夠把條形碼保存成圖片到本地,打開官網「https://www.onlinebarcodereader.com/」,上傳剛剛生成的條形碼,若是解析出的參數跟你輸入的同樣,說明條形碼能夠用。

相關文章
相關標籤/搜索