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 流、直接输出图片和保存到本地文件三种方法,最后在其他类中调用即可生成一维码。

相关推荐
Tanner_SL4 小时前
Linux笔记之常用命令
linux·笔记·php
钉先生贾16 小时前
鸿蒙启动可靠性:把首屏依赖从“同时开始”改成可降级编排【鸿蒙心迹】
华为·php·harmonyos
kingx31 天前
DASCTF BestDB 题解:双引号闭合与 union 注入回显取 flag
php·ctf·sql注入·waf绕过·union回显
FungLeo1 天前
FengCMS ·PHP+MySQL 升级实录 · 性能测量方法论:先建基线、再判定,以及为什么暂缓 UNION 改写
mysql·php·工程实践·fengcms
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 开源发布工程:GitHub Releases + SHA-256+ 三版发布说明
mysql·开源·php·fengcms
FungLeo1 天前
FengCMS · PHP+MySQL升级实录 · 存量站升级方法论:备份 → 副本验收→migrate.php→ 停写切换
android·mysql·php·工程实践·fengcms
3095566662 天前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
kingx32 天前
DASCTF 02-cookies 解题:Flappy Bird 分数 Cookie 的 Base64+gzip 构造
web安全·php·base64·gzip解压·cookie伪造
FungLeo2 天前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
wangxiaoxuan2172 天前
大模型怎样调用机器人能力:目标落地、工具调用与执行反馈
数据库·机器人·php