Home > PHP Framework > ThinkPHP > thinkphp5 + barcode method to generate barcode

thinkphp5 + barcode method to generate barcode

藏色散人
Release: 2020-04-12 14:47:18
forward
2968 people have browsed it

1. Go to the official website to download the class library "https://www.barcodebakery.com/en/download", select your own version to download

thinkphp5 + barcode method to generate barcode

Recommended tutorial:thinkphp tutorial

2. Unzip and place it under "E:\phpstudy\PHPTutorial\WWW\guahao\vendor\", where the class file is all the class files, and the barcode is generated by calling the folder In the classes, the font file is the font, index.php is a function to generate barcodes with optional conditions, and is the entrance to the main program. test_1D.php is an example of generating barcodes, and test_1D.html is the corresponding page for rendering barcodes

thinkphp5 + barcode method to generate barcode

3. We can directly use the official example (test_1D.php), copy it to where we need to use it, and then make slight changes according to our needs. Note that the path to load the third-party class library needs to be changed.

PHP 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[&#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();
    }
}
?>
Copy after login

Accept Html code to render barcode

<img  src="/static/imghw/default1.png"  data-src="{:url(&#39;createBarcode&#39;)}"  class="lazy"   alt="thinkphp5 + barcode method to generate barcode" >
Copy after login

thinkphp5 + barcode method to generate barcode

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

html code

<img  src="/static/imghw/default1.png"  data-src="{:url(&#39;createBarcode&#39;,array(&#39;text&#39;= alt="thinkphp5 + barcode method to generate barcode" >&#39;123&#39;))}"  class="lazy"  >
Copy after login

php code

change

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

to

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

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[&#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);
Copy after login

5. After generating the barcode, how to determine whether the barcode can be used? You can save the barcode as an image locally, open the official website "https://www.onlinebarcodereader.com/", and upload the just-generated barcode. If the parsed parameters are the same as what you entered, it means the barcode can be used.

thinkphp5 + barcode method to generate barcode

The above is the detailed content of thinkphp5 + barcode method to generate barcode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template