一、去官网下载类库 “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/”,上传刚刚生成的条形码,若是解析出的参数跟你输入的同样,说明条形码能够用。