经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
thinkphp5 + barcode 生成条形码
来源:cnblogs  作者:下页、再停留  时间:2019/11/12 8:49:45  对本文有异议

1、去官网下载类库 “https://www.barcodebakery.com/en/download”,选择自己的版本下载

 

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

3、我们可以直接使用官方给的例子(test_1D.php),复制到自己需要用的地方,然后根据自己的需求稍加改动即可,需要注意的是,加载第三方类库的路径需要改一下。

生成条形码的php代码<?php

  1. namespace app\index\controller;
  2. use think\Controller;
  3. /**
  4. * 条形码操作类
  5. */
  6. class Barcode extends Controller
  7. {
  8. public function createBarcode()
  9. {
  10. $class_dir = VENDOR_PATH.'barcode/class/';
  11. // Including all required classes
  12. require_once($class_dir.'BCGFontFile.php');
  13. require_once($class_dir.'BCGColor.php');
  14. require_once($class_dir.'BCGDrawing.php');
  15. require_once($class_dir.'BCGcode39.barcode.php');
  16. // Loading Font
  17. // 注意font和class是同一级文件夹
  18. $font = new \BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf', 18);// The arguments are R, G, B for color.
  19. $color_black = new \BCGColor(0, 0, 0);
  20. $color_white = new \BCGColor(255, 255, 255);
  21. $drawException = null;
  22. try {
  23. $code = new \BCGcode39();
  24. $code->setScale(2); // Resolution
  25. $code->setThickness(30); // Thickness
  26. $code->setForegroundColor($color_black); // Color of bars
  27. $code->setBackgroundColor($color_white); // Color of spaces
  28. $code->setFont($font); // Font (or 0)
         
    $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
    $code->parse($text); // Text
  1. } catch(Exception $exception) {
  2. $drawException = $exception;
  3. }
  4. /* Here is the list of the arguments
  5. 1 - Filename (empty : display on screen)
  6. 2 - Background color */
  7. $drawing = new \BCGDrawing('', $color_white);
  8. if($drawException) {
  9. $drawing->drawException($drawException);
  10. } else {
  11. $drawing->setBarcode($code);
  12. $drawing->draw();
  13. }
  14. // Header that says it is an image (remove it if you save the barcode to a file)
  15. header('Content-Type: image/png');
  16. header('Content-Disposition: inline; filename="barcode.png"');
  17. // Draw (or save) the image into PNG format.
  18. $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
  19. }
  20. public function barcodedes()
  21. {
  22. return $this->fetch();
  23. }
  24. }
  25. ?>

接受渲染条形码的Html代码

  1. <img src="{:url('createBarcode')}">

 

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

html代码

  1. <img src="{:url('createBarcode',array('text'=>'123'))}">

php代码

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

改成

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

4、如果想把条形码保存到本地,在实例化“BCGDrawing”的时候填写保存路径即可

  1. // 文件路径
  2. $file_dir = 'uploads/barcode/'.date('Y-m-d');
  3. if (!file_exists($file_dir)) {
  4. mkdir($file_dir,0755,true);
  5. }
  6. $imgUrl = $file_dir.'/'.time().'.png';
  7. $class_dir = VENDOR_PATH.'barcode/class/';
  8. // Including all required classes
  9. require_once($class_dir.'BCGFontFile.php');
  10. require_once($class_dir.'BCGColor.php');
  11. require_once($class_dir.'BCGDrawing.php');
  12. require_once($class_dir.'BCGcode39.barcode.php');
  13. // Loading Font
  14. // 注意font和class是同一级文件夹
  15. $font = new \BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf', 18);
  16. // Don't forget to sanitize user inputs
  17. // $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
  18. // The arguments are R, G, B for color.
  19. $color_black = new \BCGColor(0, 0, 0);
  20. $color_white = new \BCGColor(255, 255, 255);
  21. $drawException = null;
  22. try {
  23. $code = new \BCGcode39();
  24. $code->setScale(2); // Resolution
  25. $code->setThickness(30); // Thickness
  26. $code->setForegroundColor($color_black); // Color of bars
  27. $code->setBackgroundColor($color_white); // Color of spaces
  28. $code->setFont($font); // Font (or 0)
  29. $text = input('text'); //接收的参数
  30. $text = isset($text) ? $text :'无参数';
  31. $code->parse($text); // Text
  32. } catch(Exception $exception) {
  33. $drawException = $exception;
  34. }
  35. /* Here is the list of the arguments
  36. 1 - Filename (empty : display on screen)
  37. 2 - Background color */
  38. // 保存到本地 (路径,颜色)路径为空则表示显示到页面上
  39. $drawing = new \BCGDrawing($imgUrl, $color_white);
  40. if($drawException) {
  41. $drawing->drawException($drawException);
  42. } else {
  43. $drawing->setBarcode($code);
  44. $drawing->draw();
  45. }
  46. $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);

 

5、生成条形码之后,怎么判定条形码是否能用呢?可以把条形码保存成图片到本地,打开官网“https://www.onlinebarcodereader.com/”,上传刚刚生成的条形码,如果解析出的参数跟你输入的一样,说明条形码可以用。

 

原文链接:http://www.cnblogs.com/zxf100/p/11836276.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号