定制 alexjustesen/laravel-placehold-image 二次开发

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

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

alexjustesen/laravel-placehold-image

最新稳定版本:v1.0.2

Composer 安装命令:

composer require alexjustesen/laravel-placehold-image

包简介

A Laravel package to generate placeholder images from placehold.co

README 文档

README

A Laravel package to generate placeholder images from https://placehold.co/.

Installation

You can install the package via composer:

composer require alexjustesen/laravel-placehold-image

You can publish the config file with:

php artisan vendor:publish --tag="laravel-placehold-image-config"

This is the contents of the published config file:

return [
    'defaults' => [
        'format' => 'png',
        'background_color' => null,
        'text_color' => null,
        'text' => null,
        'font' => null,
    ],
];

Usage

Basic Usage

Generate a placeholder image URL:

use AlexJustesen\LaravelPlaceholdImage\Facades\PlaceholdImage;

// Generate a 300x200 image
$url = PlaceholdImage::generate(300, 200);
// Returns: https://placehold.co/300x200

// Generate a square 300x300 image
$url = PlaceholdImage::generate(300);
// Returns: https://placehold.co/300

Customization Options

You can customize the placeholder image with various options:

// With background color
$url = PlaceholdImage::generate(300, 200, ['background_color' => 'ff0000']);
// Returns: https://placehold.co/300x200/ff0000

// With background and text colors
$url = PlaceholdImage::generate(300, 200, [
    'background_color' => 'ff0000',
    'text_color' => '00ff00'
]);
// Returns: https://placehold.co/300x200/ff0000/00ff00

// With custom text
$url = PlaceholdImage::generate(300, 200, ['text' => 'Hello World']);
// Returns: https://placehold.co/300x200?text=Hello+World

// With different format
$url = PlaceholdImage::generate(300, 200, ['format' => 'jpg']);
// Returns: https://placehold.co/300x200.jpg

Fluent Interface

The package supports a fluent interface for method chaining:

$url = PlaceholdImage::format('jpg')
    ->backgroundColor('ff0000')
    ->textColor('ffffff')
    ->text('Custom Text')
    ->font('arial')
    ->generate(300, 200);
// Returns: https://placehold.co/300x200/ff0000/ffffff.jpg?text=Custom+Text&font=arial

Downloading Images

You can download the image data directly:

// Download image data as string
$imageData = PlaceholdImage::download(300, 200);

// Save image to file
$success = PlaceholdImage::save('/path/to/image.png', 300, 200);

Using Dependency Injection

You can also inject the service directly:

use AlexJustesen\LaravelPlaceholdImage\PlaceholdImage;

class ImageController extends Controller
{
    public function view()
    {
        $url = PlaceholdImage::generate(300, 200);

        return response()->json(['url' => $url]);
    }
}

Available Methods

generate(int $width, ?int $height = null, array $options = []): string

Generates a placeholder image URL.

download(int $width, ?int $height = null, array $options = []): string

Downloads the image data as a string.

save(string $path, int $width, ?int $height = null, array $options = []): bool

Saves the image to a file path.

Fluent Methods

  • format(string $format) - Set format (png, jpg, jpeg, webp)
  • backgroundColor(string $color) - Set background color (hex without #)
  • textColor(string $color) - Set text color (hex without #)
  • text(string $text) - Set custom text
  • font(string $font) - Set font family

Configuration

You can set default options in the config file that will be applied to all requests:

// config/placehold-image.php
return [
    'defaults' => [
        'format' => 'jpg',
        'background_color' => 'cccccc',
        'text_color' => '969696',
        'text' => null,
        'font' => 'arial',
    ],
];

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-14