php(tp5)生成条形码
因为公司业务需要,研究了一下条形码
1.下载barcodegen扩展包
官网地址:https://www.barcodebakery.com
2.下载完后解压至 extend 文件夹里面,然后复制以下代码,调用该方法,传递订单编号即可
//头部引入 use BarcodeBakery\Barcode\BCGcode128; use BarcodeBakery\Common\BCGColor; use BarcodeBakery\Common\BCGDrawing; use BarcodeBakery\Common\BCGFontFile; //生成条形码 public function txm($order_number){ import('example.vendor.autoload',EXTEND_PATH); import('packages.barcode-common.src.BCGColor',EXTEND_PATH); import('packages.barcode-common.src.BCGFontFile',EXTEND_PATH); import('packages.barcode-common.src.BCGcode128',EXTEND_PATH); import('packages.barcode-common.src.BCGDrawing',EXTEND_PATH); $colorFront = new BCGColor(0, 0, 0); $colorBack = new BCGColor(255, 255, 255); $font = new BCGFontFile(__DIR__ . '/../../../extend/example/font/Arial.ttf', 18); $code = new BCGcode128(); $code->setScale(2); // Resolution $code->setThickness(30); // Thickness $code->setForegroundColor($colorFront); // Color of bars $code->setBackgroundColor($colorBack); // Color of spaces $code->setFont($font); // Font (or 0) $code->parse($order_number); // Text $drawing = new BCGDrawing('txm/'.$order_number.'.png', $colorBack); $drawing->setBarcode($code); $drawing->draw(); $drawing->finish(BCGDrawing::IMG_FORMAT_PNG); $data = '/txm/'.$order_number.'png'; return $data; }