Index.php 1.1 KB
<?php

namespace addons\barcode\controller;

use think\addons\Controller;
use think\Response;

/**
 * 条码生成
 * @package addons\barcode\controller
 */
class Index extends Controller {

    public function index() {
        return $this->view->fetch();
    }

    // 生成条码
    public function build() {
        $text = $this->request->get('text', '1234567890');
        $type = $this->request->get('type', 'C128');
        $foreground = $this->request->get('foreground', "#000000");
        $width = $this->request->get('width', 2);
        $height = $this->request->get('height', 30);

        $params = [
            'text'           => $text,
            'type'           => $type,
            'foreground'     => $foreground,
            'width'     => $width,
            'height'     => $height,
        ];

        $barcode = \addons\barcode\library\Service::barcode($params);

        // 直接显示条码
        $response = Response::create()->header("Content-Type", "image/png");
        header('Content-Type: image/png');
        $response->content($barcode);
        return $response;
    }

}