Home  >  Article  >  PHP Framework  >  Detailed graphic explanation of thinkphp5+barcode generating barcodes

Detailed graphic explanation of thinkphp5+barcode generating barcodes

藏色散人
藏色散人forward
2021-03-03 15:47:262133browse

The following tutorial column will introduce you to thinkphp5 barcode to generate barcodes. I hope it will be helpful to friends in need! thinkphp5 barcode generate barcode

##1 , Go to the official website to download the class library "[https://www.barcodebakery.com...]", select your own version to download Detailed graphic explanation of thinkphp5+barcode generating barcodes

2, unzip and put it in "E :\phpstudy\PHPTutorial\WWW\guahao\vendor\", where the class file is all the class files, generating the barcode is to call the class in the folder, the font file is the font, and index.php is an optional condition to generate the barcode Function is the entrance to the main program. test_1D.php is an example of generating a barcode, and test_1D.html is the corresponding page for rendering barcodes

Detailed graphic explanation of thinkphp5+barcode generating barcodes

3. We can use it directly Copy the official example (test_1D.php) to where you need to use it, and then make slight changes according to your needs. It should be noted that the path to load the third-party class library needs to be changed.

Detailed graphic explanation of thinkphp5+barcode generating barcodesPHP code to generate barcode

<?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['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();
    }
}
?>
Accept Html code to render barcode

<img  alt="Detailed graphic explanation of thinkphp5+barcode generating barcodes" >

Of course, src is also Parameters can be carried, just change the following code

Detailed graphic explanation of thinkphp5+barcode generating barcodeshtml code

<img  alt="Detailed graphic explanation of thinkphp5+barcode generating barcodes" >'123'))}">

php code and change

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

$text = input('text');      //接收的参数
4. If you want to save the barcode locally, just fill in the save path when instantiating "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. After generating the barcode, how to determine whether the barcode can be used? ?

You can save the barcode as a picture locally, open the official website "[https://www.barcodebakery.com/en/download]", and upload the just-generated barcode. If the parsed parameters match the ones you entered Same, indicating that the barcode can be used.

Detailed graphic explanation of thinkphp5+barcode generating barcodesRelated recommendations:

The latest 10 thinkphp video tutorials

The above is the detailed content of Detailed graphic explanation of thinkphp5+barcode generating barcodes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete