定制 badrshs/laravel-dynamic-image-composer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

badrshs/laravel-dynamic-image-composer

Composer 安装命令:

composer require badrshs/laravel-dynamic-image-composer

包简介

Compose images dynamically from templates with text and image overlays

README 文档

README

Compose images dynamically from templates with text and image overlays. Perfect for generating certificates, badges, social media graphics, or any dynamic image content.

Features

  • 🎨 Visual Designer Interface - Drag-and-drop template designer with live preview
  • 📝 Dynamic text overlays with custom fonts, colors, and positioning
  • 🖼️ Image overlay support with opacity and positioning
  • 🌍 Multi-language support (Arabic, English, and more)
  • 📐 Flexible positioning (left, center, right alignment)
  • 🎯 Template-based generation with database storage
  • 🔧 Filament admin panel integration (optional)
  • 💾 Multiple storage disk support
  • ⚡ Simple, fluent API

Installation

Install via Composer:

composer require badrshs/laravel-dynamic-image-composer

Quick Install (Recommended)

Run the install command which will publish config, migrations, views, and optionally fonts:

php artisan dynamic-image-composer:install --with-fonts

This command will:

  • ✅ Publish configuration file
  • ✅ Publish migrations
  • ✅ Publish views
  • ✅ Publish default fonts (with --with-fonts flag)
  • ✅ Create necessary directories
  • ✅ Run migrations (with confirmation)
  • ✅ Create storage link

Manual Installation

If you prefer manual installation:

php artisan vendor:publish --tag=dynamic-image-composer-config
php artisan vendor:publish --tag=dynamic-image-composer-migrations
php artisan vendor:publish --tag=dynamic-image-composer-views
php artisan vendor:publish --tag=dynamic-image-composer-fonts
php artisan migrate
php artisan storage:link

Quick Start

Visual Designer Interface

The package includes a complete drag-and-drop visual designer for creating and editing templates:

  1. Create a template via Filament or directly in the database
  2. Click the "Designer" button on any template
  3. Upload image elements (logos, stamps, decorations)
  4. Drag and position elements on the canvas
  5. Add text fields with positioning and styling
  6. Preview your design in real-time
  7. Generate the final composite image

Accessing the Designer:

// From Filament: Click "Designer" button on any template
// Or directly via route:
route('image-template.designer', ['template' => $templateId])

Basic Usage

use Badrshs\DynamicImageComposer\DynamicImageComposer;

$composer = new DynamicImageComposer();

// Generate image with text overlays
$image = $composer->generate(
    templatePath: 'templates/my-template.png',
    fields: [
        'name' => [
            'value' => 'John Doe',
            'x' => 'center',
            'y' => 500,
            'fontSize' => 60,
            'color' => 'black',
            'font' => 'default',
            'alignment' => 'center'
        ],
        'date' => [
            'value' => date('Y-m-d'),
            'x' => 100,
            'y' => 1000,
            'fontSize' => 30,
            'color' => 'gray',
            'font' => 'default'
        ]
    ]
);

// Save to storage
$result = $composer->save($image, 'output-' . time() . '.png');
// Returns: ['path' => '...', 'url' => '...', 'filename' => '...', 'disk' => '...']

// Or output directly as HTTP response
return $composer->output($image, 'my-image.png');

With Image Overlays

$image = $composer->generate('templates/base.png', [
    'title' => [
        'value' => 'Certificate of Achievement',
        'x' => 'center',
        'y' => 300,
        'fontSize' => 80,
        'color' => 'gold'
    ]
]);

// Add logo overlay
$composer->addOverlay($image, 'logos/company-logo.png', [
    'x' => 100,
    'y' => 100,
    'width' => 200,
    'height' => 200,
    'opacity' => 0.8
]);

return $composer->output($image);

Using Database Templates

use Badrshs\DynamicImageComposer\Models\ImageTemplate;

// Create a template
$template = ImageTemplate::create([
    'name' => 'My Certificate Template',
    'background_image' => 'templates/certificate-bg.png',
    'width' => 2480,
    'height' => 3508,
    'is_active' => true,
    'field_configuration' => [
        'fields' => [
            'name' => [
                'x' => 'center',
                'y' => 1200,
                'fontSize' => 100,
                'color' => 'black',
                'font' => 'monotype',
                'alignment' => 'center'
            ]
        ]
    ]
]);

// Generate from template
$composer = new DynamicImageComposer();
$image = $composer->generate(
    $template->background_image,
    array_merge(
        ['name' => ['value' => 'Jane Smith']],
        $template->field_configuration['fields'] ?? []
    )
);

Configuration

Edit config/dynamic-image-composer.php:

return [
    // Storage disk for templates and generated images
    'disk' => env('DYNAMIC_IMAGE_DISK', 'public'),
    
    // Directories
    'templates_directory' => 'image-templates',
    'elements_directory' => 'image-elements',
    'generated_directory' => 'generated-images',
    'fonts_directory' => 'fonts',
    
    // Font definitions (add your custom fonts)
    'fonts' => [
        'default' => [
            'en' => 'Museo500-Regular.ttf',
            'ar' => 'sky.ttf',
        ],
        // Add more fonts...
    ],
    
    // Color definitions (RGB values)
    'colors' => [
        'black' => [40, 40, 40],
        'white' => [255, 255, 255],
        'gold' => [212, 175, 55],
        // Add more colors...
    ],
];

Field Configuration

Each field supports these options:

Option Type Description Default
value string Text to display Required
x int|'center' X position or 'center' 0
y int Y position 0
fontSize int Font size 20
color string Color name from config 'black'
font string Font name from config 'default'
alignment string 'left', 'center', 'right' 'left'

Filament Integration (Optional)

If you're using Filament, register the resource in your panel:

use Badrshs\DynamicImageComposer\Filament\Resources\ImageTemplateResource;

public function panel(Panel $panel): Panel
{
    return $panel
        ->resources([
            ImageTemplateResource::class,
        ]);
}

This provides a full admin interface for managing templates and elements, including:

  • Template CRUD operations
  • Visual designer interface
  • Element management
  • Live preview generation

Displaying Generated Images

Use the included Blade component to display generated images in a grid:

<x-dynamic-image-composer::generated-images-grid 
    :images="$generatedImages"
    itemLabel="Certificate"
/>

Where $generatedImages is an array of:

[
    [
        'url' => 'https://...',
        'name' => 'John Doe',
        'filename' => 'certificate.png',
        'metadata' => 'Generated on 2024-01-01' // optional
    ],
    // ...
]

Advanced Usage

Custom Fonts

Add custom fonts to your public/fonts directory or storage, then register them in config:

'fonts' => [
    'my-font' => [
        'en' => 'MyFont-Regular.ttf',
        'ar' => 'MyFont-Arabic.ttf',
    ],
],

Custom Colors

Add custom colors in config:

'colors' => [
    'brand-blue' => [30, 144, 255],
    'brand-red' => [220, 53, 69],
],

Arabic Text Support

The package automatically detects and handles Arabic text:

$image = $composer->generate('template.png', [
    'name' => [
        'value' => 'محمد أحمد', // Arabic text
        'x' => 'center',
        'y' => 500,
        'fontSize' => 60,
        'font' => 'default', // Will use Arabic font variant
    ]
]);

Use Cases

  • 📜 Certificates and diplomas
  • 🏆 Achievement badges
  • 🎫 Event tickets
  • 📱 Social media graphics
  • 🎴 ID cards and passes
  • 📊 Dynamic reports with charts
  • 🎨 Personalized marketing materials

Requirements

  • PHP 8.1 or higher
  • Laravel 10.x, 11.x, or 12.x
  • GD Library (enabled by default in most PHP installations)
  • Default fonts included (Roboto for English, Noto Kufi Arabic for Arabic)

License

MIT License. See LICENSE file for details.

Credits

Developed by Badr Shs

badrshs/laravel-dynamic-image-composer 适用场景与选型建议

badrshs/laravel-dynamic-image-composer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「template」 「composer」 「dynamic」 「laravel」 「image-generation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 badrshs/laravel-dynamic-image-composer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 badrshs/laravel-dynamic-image-composer 我们能提供哪些服务?
定制开发 / 二次开发

基于 badrshs/laravel-dynamic-image-composer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-15