jorgenwdm / laravel-barcode
基于TCPDF条码库的Laravel条码生成器
dev-main
2021-11-24 00:09 UTC
Requires
- php: ^8.0
This package is auto-updated.
Last update: 2024-08-29 06:01:48 UTC
README
要求 | 安装 | 支持的符号Laravel Barcode
简介
Laravel Barcode是一个基于TCPDF条码函数的包装器,专为Laravel平台构建。该项目支持31种一维条码符号和3种二维条码符号
要求
- PHP >= 8.0
- Laravel >= 8.6.6
安装
安装非常简单。
首先您需要安装此软件包
composer require jorgenwdm/laravel-barcode
然后您可以发布此软件包的配置文件(目前为空)。
php artisan vendor:publish --provider="Jorgenwdm\Barcode\BarcodeServiceProvider"
注意:我们的配置文件是laravel-barcode.php
,您可以在Laravel的config
文件夹中找到它。
然后我们就可以开始了!
一些重要提示
每个条码,一维或二维,都由元素组成。一维条码由线条组成,二维条码由矩形组成。当我们设置条码的大小时,我们设置每个元素的大小(而不是整个条码的大小)。
此外,当我们设置颜色时,这是条码每个元素的颜色。条码的背景始终是透明的。
支持的符号
如何创建一维条码
使用默认设置创建条码的方法非常简单。
支持四种输出方法:toHtml()
、toSvgCode()
、toPng()
、toSvg()
。
示例
use Jorgenwdm\Barcode\Generators\Barcode1d; echo Barcode1d::create("C39", "12345678")->toHtml();
use Jorgenwdm\Barcode\Generators\Barcode1d; echo "<svg height=174 width=174>"; echo Barcode1d::create("C39", "12345678")->toSvgCode(); echo "</svg>";
use Jorgenwdm\Barcode\Generators\Barcode1d; $content = Barcode1d::create("C39", "12345678")->toPng(); return response($content)->header('Content-Type', 'image/png');
use Jorgenwdm\Barcode\Generators\Barcode1d; $content = Barcode1d::create("C39", "12345678")->toSvg(); return response($content)->header('Content-Type', 'image/svg+xml');
此外,还有一些辅助方法可以自定义最终要渲染的条码。
这些是size($width, $height)
和color($hexColor)
。
use Jorgenwdm\Barcode\Generators\Barcode1d; // We will create a C39 barcode where each line width is 2px and height is 30px // The color of the barcode will be #000000 (that is black) echo Barcode1d::create("C39", "12345678")->size(2,30)->color("#000000")->toHtml();
如何创建二维条码
使用默认设置创建条码的方法非常简单。
支持四种输出方法:toHtml()
、toSvgCode()
、toPng()
、toSvg()
。
示例
use Jorgenwdm\Barcode\Generators\Barcode2d; echo Barcode2d::create("DATAMATRIX", "http://www.test.com/")->toHtml();
use Jorgenwdm\Barcode\Generators\Barcode2d; echo "<svg height=174 width=174>"; echo Barcode2d::create("QRCODE", "http://www.test.com/")->toSvgCode(); echo "</svg>";
use Jorgenwdm\Barcode\Generators\Barcode2d; $content = Barcode2d::create("DATAMATRIX", "http://www.test.com/")->toPng(); return response($content)->header('Content-Type', 'image/png');
use Jorgenwdm\Barcode\Generators\Barcode2d; $content = Barcode2d::create("QRCODE", "http://www.test.com/")->toSvg(); return response($content)->header('Content-Type', 'image/svg+xml');
此外,还有一些辅助方法可以自定义最终要渲染的条码。
这些是size($width, $height)
和color($hexColor)
。
use Jorgenwdm\Barcode\Generators\Barcode2d; // We will create a DATAMATRIX barcode where // each small rectangle of the barcode has width 6px and height 6px // The color of the barcode will be #000000 (that is black) echo Barcode2d::create("DATAMATRIX", "http://www.in.gr/")->size(6,6)->color("#000000")->toHtml();
辅助函数
我们还创建了一个辅助函数,该函数封装了条码生成器的基本功能。该函数在您安装此软件包后自动加载,您可以在项目中全局使用它。
基本语法是
create_barcode($barcodeCategory, $barcodeType, $barcodeText, $width, $height, $hexColor, $output);
示例
echo create_barcode("1D", "C39", "GEORGE", 5, 30, "#000000", "HTML");
许可证
此项目是开源软件,根据MIT许可证授权。