_Account = $this->getKey($this->_apiCode); //验证是否具有访问这个接口的权限 if(!isset($this->_Account['isBarcode'])){ HelperService::returnJson(['code'=>400,'msg'=>'isBarcode interface unauthorized access','data'=>[]]); } } /** * 条形码 * @throws \Exception */ public function Bar(){ header( "Content-Type:text/html;charset=UTF-8"); $params = $this->_sysParams; $rule = [ 'content|内容'=>'require', 'is_down|是否下载'=>'require', ]; $validate = new Validate($rule); if(!$validate->check($params)){ HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]); } // Including all required classes require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGFontFile.php"); require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGDrawing.php"); require_once(WEB_ROOT."/../vendor/barcodegen/class/BCGcode128.barcode.php"); // Loading Font $font = new \BCGFontFile('./font/Arial.ttf', 8); // Don't forget to sanitize user inputs $content = $params['content']; // 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 \BCGcode128(); $code->setScale(1); // 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) $code->parse($content); // content } catch(\Exception $ex) { throw new \Exception($ex->getMessage()); } /* 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(); } if($params['is_down']){ $time = time().rand(1000,9999); $png = "./qrcode/{$time}.png"; $drawing->setFilename($png); $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG); $fontSize = 18; if(isset($params['note']) && !empty($params['note'])){ $noteArr = explode(',',$params['note']); //以每行行高22px计算+20的空间 $total_lineHeight = count($noteArr)*30-0; $imageSource = imagecreatefrompng($png); $width = imagesx($imageSource); $height = imagesy($imageSource); //创建画板 $Palette = @imagecreate($width,$height+$total_lineHeight); //填充白色背景 imagecolorallocate($Palette,255,255,255); imagecopyresized($Palette,$imageSource,0,0,0,0,$width,$height,$width,$height); //指定字体的颜色 $color = imagecolorallocate($Palette,0,0,0); $i = 0; foreach($noteArr as $noteLine){ $fontBox = imagettfbbox($fontSize, 0, './font/NotoSansHans-DemiLight.ttf', $noteLine);//水平居中实质 imagettftext($Palette,$fontSize,0,ceil(($width - $fontBox[2]) / 2),$height+30*($i++)+15,$color,'./font/NotoSansHans-DemiLight.ttf',$noteLine); } imagepng($Palette, $png); } HelperService::downloadFile($png); } // 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); } }