>  기사  >  PHP 프레임워크  >  thinkphp5 + 바코드 생성 방법

thinkphp5 + 바코드 생성 방법

藏色散人
藏色散人앞으로
2020-04-12 14:47:182684검색

1. 공식 웹사이트로 이동하여 클래스 라이브러리 "https://www.barcodebakery.com/en/download"를 다운로드하고 원하는 버전을 선택하여 다운로드하세요

thinkphp5 + 바코드 생성 방법

추천 튜토리얼: thinkphp 튜토리얼

2 압축을 풀고 "E:phpstudyPHPTutorialWWWguahaovendor"에 넣으시면 됩니다. 여기서 클래스 파일은 모두 클래스 파일이고, 바코드 생성은 폴더에 있는 클래스를 호출하는 것이고, 글꼴 파일은 글꼴이고, index.php는 선택적 조건으로 바코드를 생성하는 함수이며, 메인 프로그램인 test_1D의 입구입니다. PHP는 바코드 생성 예제이고, test_1D.html은 바코드 렌더링에 해당하는 페이지입니다

thinkphp5 + 바코드 생성 방법

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 + 바코드 생성 방법" >

thinkphp5 + 바코드 생성 방법

물론 src도 매개변수를 전달할 수 있습니다. 다음 코드를 변경하세요

html 코드

<img  src="{:url(&#39;createBarcode&#39;,array(&#39;text&#39;= alt="thinkphp5 + 바코드 생성 방법" >&#39;123&#39;))}">

php 코드

Put

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

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

로 변경되었습니다. 4. 바코드를 로컬에 저장하려면 "BCG Drawing"

// 文件路径
        $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 + 바코드 생성 방법

위 내용은 thinkphp5 + 바코드 생성 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제