mprince / laravel-barcode-generator
在 Laravel 8 中生成条形码
dev-main
2021-10-13 06:45 UTC
Requires
- laravel/framework: 5.2.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.*
This package is auto-updated.
Last update: 2024-09-13 13:31:20 UTC
README
本软件包使用 Laravel 和核心 PHP 生成不同类型的条形码。
注意:对于此软件包,您必须启用 gd 库。
安装
在项目根目录下,打开您的终端
composer require mprince/laravel-barcode-generator
Composer 会自动下载所有依赖项。
对于 Laravel
完成安装后,打开配置文件夹中的 app.php,将以下行粘贴到 providers 数组中
peal\barcodegenerator\BarcodeServiceProvider::class,
为了支持 Facade,将以下行粘贴到 aliases 数组中
'BarCode' => peal\barcodegenerator\Facades\BarCode::class,
用法
//Generate into barcode folder under public $bar = App::make('BarCode'); $barcodes = [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ]; $barcontent = $bar->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; //Generate into customize folder under public $bar = App::make('BarCode'); $barcodes = [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg', 'filepath' => 'prdbarcode' ]; $barcontent = $bar->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'], $filepath = $barcode['filepath'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>';
多个条形码
//Generate into barcode folder under public $bar = App::make('BarCode'); $barcodes = [ [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image2.jpeg' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image3.jpeg' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image4.jpeg' ], ]; foreach($barcodes as $barcode) { $barcontent = $bar->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; } //Generate into customize folder under public $bar = App::make('BarCode'); $barcodes = [ [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg', 'filepath' => 'prdbarcode' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image2.jpeg', 'filepath' => 'prdbarcode' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image3.jpeg', 'filepath' => 'prdbarcode' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image4.jpeg', 'filepath' => 'prdbarcode' ], ]; foreach($barcodes as $barcode) { $barcontent = $bar->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'], $filepath = $barcode['filepath'], )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; }
使用 Facade
use peal\barcodegenerator\Facades\BarCode; //Single barcode //Generate into barcoce folder under public $barcodes = [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ]; $barcontent = BarCode::barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; //Generate into customize folder under public $barcodes = [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg', 'filepath' => 'prdbarcode' ]; $barcontent = BarCode::barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'], $filepath = $barcode['filepath'], )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; //Multiple barcode /** * For customize folder name, use filepath key and parameter */ $barcodes = [ [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image2.jpeg' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image3.jpeg' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image4.jpeg' ], [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image5.jpeg' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image6.jpeg' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image7.jpeg' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image8.jpeg' ], ]; foreach($barcodes as $barcode) { $barcontent = BarCode::barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; }
对于核心 PHP
use peal\barcodegenerator\Server\BarCodeServer; use peal\barcodegenerator\BarCode; //single barcode $barcodes = [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ]; $barcontent = new BarCodeServer(new BarCode()); $barcontent = $barcontent->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; //Multiple barcode $barcodes = [ [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image1.jpeg' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image2.jpeg' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image3.jpeg' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image4.jpeg' ], [ 'text' => 'HelloHello', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image5.jpeg' ], [ 'text' => 'HelloPeal', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image6.jpeg' ], [ 'text' => 'Hi Ruhul', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code128b', 'print' => true, 'sizefactor' => 1, 'filename' => 'image7.jpeg' ], [ 'text' => 'HelloMahian', 'size' => 50, 'orientation' => 'horizontal', 'code_type' => 'code39', 'print' => true, 'sizefactor' => 1, 'filename' => 'image8.jpeg' ], ]; $barcontent = new BarCodeServer(new BarCode()); foreach($barcodes as $barcode) { $barcontent = $barcontent->barcodeFactory()->renderBarcode( $text=$barcode["text"], $size=$barcode['size'], $orientation=$barcode['orientation'], $code_type=$barcode['code_type'], // code_type : code128,code39,code128b,code128a,code25,codabar $print=$barcode['print'], $sizefactor=$barcode['sizefactor'], $filename = $barcode['filename'] )->filename($barcode['filename']); echo '<img alt="testing" src="'.$barcontent.'"/>'; }