2、解压放到“E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,index.php是一个可选择条件生成条形码的功能,是主程序的入口,test_1D.php是给的生成条形码的例子,test_1D.html是对应的渲染条形码的页面

3、我们可以直接使用官方给的例子(test_1D.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) 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();

}

}

?>

接受渲染条形码的Html代码

当然,src还可以携带参数,只需更改以下代码

html代码

php代码把

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

改成

$text = input('text'); //接收的参数

4、如果想把条形码保存到本地,在实例化“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、生成条形码之后,怎么判定条形码是否能用呢?

可以把条形码保存成图片到本地,打开官网“

php 批量生成一维码,thinkphp5 + barcode 生成条形码相关推荐

  1. c语言 生成一维条形码,C# BarcodeLib。dll 生成一维码的问题

    public Image GetImage(int height,int width, BarcodeLib.TYPE type,string code)         {             ...

  2. php 生产一维码,透过 PHP 生成 一维码

    通过 PHP 生成 一维码 此代码来自 http://www.nixiaofeng.com/110.html 13) die('条码长度不正确'); if (strlen($code) == 12) ...

  3. java生成一维码和二维码

    这两天想了解一下二维码是怎样生成的.然后在网上看了很多资料,也有很多源码可以直接用的.我也没有自己写,也是拿着源码进行看和修改的,然后生成自己想要的二维码和一维码,还是很不错的,所以分享一下. 首先第 ...

  4. java 打印一维码_java生成一维码

    /** * * 一维码图片生成器 * @author winter.liu * */ public class OneBarcodeUtil { /** * 生成一维码 * @param value ...

  5. Qt zint (生成一维码/二维码)

    zint 编译 源码添加到工程 生成一维码 生成DM码 DM码识别 下载源码1: (笔者下载最新的zint-master.zip(20210126),不清楚是不是官方维护的github仓库) http ...

  6. 用java生成一维码

    1.把一个订单或者商品编号生成一维码 [java] view plaincopy import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.i ...

  7. C# 实现生成一维码、二维码

    注意 :需要使用以下库文件 using ThoughtWorks.QRCode.Codec; using ZXing; using ZXing.Common; using ZXing.QrCode; ...

  8. java生成一维码和二维码,mybatis原理和实现机制

    } } return image; } /** 将字符串编成一维条码的矩阵 @param str @param width @param height @return */ public static ...

  9. js生成一维码和二维码

    实现效果:页面同时显示一维条形码和二维码 引入一维码和二维码的js生成文件:barcode 和 qrcode <script type="text/javascript" s ...

  10. C# 生成一维码(条形码)和二维码

    前言 在使用C#语言进行客户端软件开发时很多时候我们会有需要生成二维码和一维码的需求,在智能工厂和一些商品扫码之类的软件中应用居多,此文章简单记录了一维码和二维码的编码格式和使用ZXing库生成二维码 ...

最新文章

  1. 30秒或更短的时间内弄懂的有用CSS代码片段
  2. 13.在Ubuntu20.04下,chromium无法输入中文以及如何卸载chromium浏览器
  3. java 图像倾斜角度计算_【干货】无人机航测倾斜实景三维建模进行土方计算(基于Smart3D全面讲解)...
  4. 【python 2】python 进阶
  5. android多点触摸手势,安卓手势学习笔记(三) 多点触控
  6. 异步生成器_使用生成器实现异步并等待
  7. centos + nodejs + egg2.x 开发微信分享功能
  8. iOS核心动画 - CALayer
  9. 有关UITableviewCell 重用内存 内部解析
  10. 【读书笔记《Android游戏编程之从零开始》】7.Android 游戏开发常用的系统控件(Dialog)
  11. Android 集成微信h5支付
  12. 02-Spring的核心API
  13. 批量获取百度网盘文件目录
  14. CentOS安装配置freeIPA
  15. STM32f429开发中USB读写文件涉及到的库移植
  16. FreeRTOS学习(一)
  17. 如何快速的登陆github
  18. 不学编程都能看懂的@Transactional()事务
  19. 扫地机器人(简单路径规划)
  20. M2M系统是什么,你知道吗?

热门文章

  1. python flask服务器假死_IE浏览器访问Flask自带服务器假死问题解决方法 - digwtx
  2. ppt助手网络服务器如何设置在哪里,网络服务器配置与管理ppt课件.ppt
  3. excel文件如何解密工作表保护密码
  4. 2019携程校招笔试
  5. iOS 单元测试 Tests 和 UITests
  6. H5 实现类似QQ消息列表(已读,未读)拖拽点击事件功能
  7. php怎么黑admin管理员密码是什么,tplink管理员密码是什么?
  8. c++11:nlohmann::json进阶使用(二)应用adl_serializer解决第三方数据类型(such as uri)的序列化和反序列化
  9. Linux下键盘测试工具
  10. RouterOS 6.48.6 安装与配置