Fastadmin 框架生成条形码

本文介绍了在 PHP 框架中集成一维码生成功能的方法。首先通过 Composer 安装 picqer/php-barcode-generator 三方类库,然后在 common/library 下封装 Barcode 类,提供获取 PNG 流、直接输出图片和保存到本地文件三种方法,最后在其他类中调用即可生成一维码。

目录

安装三方类库

封装类库

调用

总结


安装三方类库

命令

bash 复制代码
composer require picqer/php-barcode-generator:2.2.0

安装完成,版本为2.4

封装类库

在common/library下创建Barcode.php,以备框架中调用,

内容如下:

php 复制代码
<?php
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;
use Picqer\Barcode\BarcodeGenerator;
class Barcode
{
    /**
     * 一维码类型常量映射,外部直接调用
     */
    const CODE_128 = BarcodeGenerator::TYPE_CODE_128;
    const EAN_13   = BarcodeGenerator::TYPE_EAN_13;
    const CODE_39  = BarcodeGenerator::TYPE_CODE_39;
    const UPC_A    = BarcodeGenerator::TYPE_UPC_A;
/**
     * 获取一维码PNG二进制流
     * @param string $code 条码内容
     * @param string $type 条码类型 BarcodeHelper::CODE_128
     * @param int $widthFactor 单条宽度
     * @param int $height 条码高度
     * @param array $color 条码颜色 [R,G,B]
     * @return string PNG图片二进制
     */
    public static function getBarcodeStream(
        string $code,
        string $type = self::CODE_128,
        int $widthFactor = 2,
        int $height = 90,
        array $color = [0, 0, 0]
    ): string {
        $generator = new BarcodeGeneratorPNG();
        // 强制使用GD库,避免imagick缺失报错(phpstudy默认只有gd)
        $generator->useGd();
return $generator->getBarcode($code, $type, $widthFactor, $height, $color);
    }
/**
     * 直接输出图片到浏览器(接口使用)
     * @param string $code
     * @param string $type
     * @return void
     */
    public static function outputBarcode(
        string $code,
        string $type = self::CODE_128
    ): void {
        $stream = self::getBarcodeStream($code, $type);
        header('Content-Type:image/png');
        echo $stream;
        exit;
    }
/**
     * 保存条码到服务器本地文件
     * @param string $code 条码内容
     * @param string $savePath 保存完整路径 ./storage/barcode/xxx.png
     * @param string $type
     * @return bool
     */
    public static function saveBarcode(
        string $code,
        string $savePath,
        string $type = self::CODE_128
    ): bool {
        $stream = self::getBarcodeStream($code, $type);
        // 自动创建目录
        $dir = dirname($savePath);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        return file_put_contents($savePath, $stream) !== false;
    }
}

调用

在其他类中调用这个一维码类,并设置文件名称和路径,如下:

php 复制代码
$obj = new \app\common\library\Barcode();
$savePath = '/uploads/barcode/'.time().'.png';

总结

本文介绍了在 PHP 框架中集成一维码生成功能的方法。首先通过 Composer 安装 picqer/php-barcode-generator 三方类库,然后在 common/library 下封装 Barcode 类,提供获取 PNG 流、直接输出图片和保存到本地文件三种方法,最后在其他类中调用即可生成一维码。

相关推荐
IM架构师19 小时前
# 一次老旧PHPCMS网站入侵排查:从SQL注入到PHP后门,以及IP白名单止血
sql·tcp/ip·php
码途行者20 小时前
处理超时未支付订单:两套轻量化方案,附 PHP 极简实现
开发语言·redis·php
vx_Biye_Design20 小时前
PHP中医养生馆预约系统60841-计算机课程设计、毕业设计
vue.js·spring boot·后端·mysql·php·课程设计·express
.Bryce.1 天前
你了解 Blackboard(黑板系统)吗?听说过基于黑板系统实现的智能体吗?
java·网络·gpt·php·agent·codex·黑板系统
yu_zhidun1 天前
企业电脑监控软件怎么选:别只看功能清单,先看这 6 个底层能力
开发语言·电脑·php·域智盾系统
李纲明1 天前
WordPress 白屏排查手册:关插件、换主题、开调试、查 PHP(附停手信号)
开发语言·php·wordpress·故障排查·白屏·插件冲突
天远API1 天前
零信任架构实战:基于天远公安二要素政务版构建自动化不动产交易与契税核算实名核实网关
人工智能·架构·自动化·php
WeiXin_DZbishe2 天前
基于springboot装修风格选择系统的设计与开发-计算机毕设【课程设计】77964
java·vue.js·spring boot·后端·python·php·课程设计
anew___2 天前
《从零手写操作系统 (49):虚拟内存进阶——Demand Paging、Swap与缺页异常处理》
linux·服务器·php
misty youth2 天前
Navicat与PHPstudy小皮
mysql·adb·php·navicat