Barcode.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use think\Log;
  5. use think\Validate;
  6. /**
  7. * 条形维码
  8. * Class Alipay
  9. * @package app\expand\controller
  10. */
  11. class Barcode extends BaseAuth
  12. {
  13. private $_Account = null;
  14. public function __construct(){
  15. parent::__construct();
  16. $this->_Account = $this->getKey($this->_apiCode);
  17. //验证是否具有访问这个接口的权限
  18. if(!isset($this->_Account['isBarcode'])){
  19. HelperService::returnJson(['code'=>400,'msg'=>'isBarcode interface unauthorized access','data'=>[]]);
  20. }
  21. }
  22. /**
  23. * 条形码
  24. * @throws \Exception
  25. */
  26. public function Bar(){
  27. header( "Content-Type:text/html;charset=UTF-8");
  28. $params = $this->_sysParams;
  29. $rule = [
  30. 'content|内容'=>'require',
  31. 'is_down|是否下载'=>'require',
  32. ];
  33. $validate = new Validate($rule);
  34. if(!$validate->check($params)){
  35. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  36. }
  37. // Including all required classes
  38. require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGFontFile.php");
  39. require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGDrawing.php");
  40. require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGcode128.barcode.php");
  41. // Loading Font
  42. $font = new \BCGFontFile('./font/Arial.ttf', 8);
  43. // Don't forget to sanitize user inputs
  44. $content = $params['content'];
  45. // The arguments are R, G, B for color.
  46. $color_black = new \BCGColor(0, 0, 0);
  47. $color_white = new \BCGColor(255, 255, 255);
  48. $drawException = null;
  49. try {
  50. $code = new \BCGcode128();
  51. $code->setScale(1); // Resolution
  52. $code->setThickness(30); // Thickness
  53. $code->setForegroundColor($color_black); // Color of bars
  54. $code->setBackgroundColor($color_white); // Color of spaces
  55. $code->setFont($font); // Font (or 0)
  56. $code->parse($content); // content
  57. } catch(\Exception $ex) {
  58. throw new \Exception($ex->getMessage());
  59. }
  60. /* Here is the list of the arguments
  61. 1 - Filename (empty : display on screen)
  62. 2 - Background color */
  63. $drawing = new \BCGDrawing('', $color_white);
  64. if($drawException) {
  65. $drawing->drawException($drawException);
  66. } else {
  67. $drawing->setBarcode($code);
  68. $drawing->draw();
  69. }
  70. if($params['is_down']){
  71. $time = time().rand(1000,9999);
  72. $png = "./qrcode/{$time}.png";
  73. $drawing->setFilename($png);
  74. $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
  75. $fontSize = 18;
  76. if(isset($params['note']) && !empty($params['note'])){
  77. $noteArr = explode(',',$params['note']);
  78. //以每行行高22px计算+20的空间
  79. $total_lineHeight = count($noteArr)*30-0;
  80. $imageSource = imagecreatefrompng($png);
  81. $width = imagesx($imageSource);
  82. $height = imagesy($imageSource);
  83. //创建画板
  84. $Palette = @imagecreate($width,$height+$total_lineHeight);
  85. //填充白色背景
  86. imagecolorallocate($Palette,255,255,255);
  87. imagecopyresized($Palette,$imageSource,0,0,0,0,$width,$height,$width,$height);
  88. //指定字体的颜色
  89. $color = imagecolorallocate($Palette,0,0,0);
  90. $i = 0;
  91. foreach($noteArr as $noteLine){
  92. $fontBox = imagettfbbox($fontSize, 0, './font/NotoSansHans-DemiLight.ttf', $noteLine);//水平居中实质
  93. imagettftext($Palette,$fontSize,0,ceil(($width - $fontBox[2]) / 2),$height+30*($i++)+15,$color,'./font/NotoSansHans-DemiLight.ttf',$noteLine);
  94. }
  95. imagepng($Palette, $png);
  96. }
  97. HelperService::downloadFile($png);
  98. }
  99. // Header that says it is an image (remove it if you save the barcode to a file)
  100. header('Content-Type: image/png');
  101. header('Content-Disposition: inline; filename="barcode.png"');
  102. // Draw (or save) the image into PNG format.
  103. $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
  104. }
  105. }