说明:这是一个php教程_thinkphp5+barcode,讲解如何生成条形码。 如果你想学习寻找类似的文章,可以进入php教程获取最新的优质资讯。 1...
这是一个php教程_thinkphp5+barcode,讲解如何生成条形码。 如果你想学习寻找类似的文章,可以进入php教程获取最新的优质资讯。 1.去官网下载解释器“”,选择自己的版本进行下载
推荐教程:thinkphp教程
2、解压后放在“E:phpstudyPHPTutorialWWWguahaovendor”下php生成条形码,其中class文件都是class文件,生成条形码是调用文件夹里的class,font文件是font, index.php是生成条形码的函数,可选条件是主程序的入口,test_1D.php是生成条形码的示例,test_1D.html是渲染条形码的对应页面
3、我们可以直接使用官方的例子(test_1D.php),复制到我们需要使用的地方,然后根据自己的需要进行修改。 需要注意的是,加载第三方解释器的路径需要改变。 。
生成条形码的php代码
<?php namespace appindexcontroller; use thinkController; /** * 条形码操作类 */ 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) 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 代码
其实src也可以带参数,修改如下代码即可
html代码
'123'))}">
php代码
捆
$text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
变成
$text = input('text'); //接收的参数
4、如果想将条码保存在本地php生成条形码,可以在实例化“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 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、条码生成后,如何判断条码是否可以使用? 您可以将条码保存为图片本地,打开官网“”,上传刚刚生成的条码。 如果解析出的参数与您输入的一致,则说明该条码可以使用。
发表评论