tzsk/collage

使用 Laravel 生成图片拼贴

7.0.0 2023-05-06 08:51 UTC

README

Collage Cover Image

GitHub License Latest Version on Packagist GitHub Tests Action Status Total Downloads

现在使用 PHP 轻松创建图片拼贴。此包使用 intervention/image 包以利用图像处理。

使用此包非常简单,创建漂亮的拼贴也不再困难。

注意: 目前此包仅支持 4 张图片。如果您喜欢,可以编写自己的生成器以添加 5 张。

📦 安装

这是一个 composer 包。因此,只需在您的项目目录中运行以下 composer 命令即可进行安装。

composer require tzsk/collage

⚙️ 配置

如果您在使用此包时不在 Laravel 中,则无需执行此步骤。

config/app.php

此包支持包自动发现。最新版本仅支持 Laravel >= 7.x

如果您想使用除 'gd' 之外的其他驱动程序,则必须发布配置文件

php artisan collage:publish

然后,您将在配置目录中有一个文件:config/collage.php

如果您使用 'imagick',则可以更改它。

🔥 使用方法

首先,您需要有一组图片来制作拼贴。此包可以接受多种类型的目标。

$images = [
    // List of images
    'images/some-image.jpg',
];

支持其他类型的图像目标

$images = [
    // 1. File Contents
    file_get_contents('some/image/path/or/url'),

    // 2. Direct URLs
    'https://some/image/url',

    // 3. Absolute & Relative Path
    '/some/image/somewhere/on/disk',

    // 4. Intervention\Image\Image Object
    ImageManagerStatic::make('...'),
    // It's Intervention\Image\ImageManagerStatic

    // 5. Or if you are Using Laravel
    Image::make('...'),
    // It's Intervention\Image\Facades\Image
];

根据数组中的图像数量,此包将自动使用正确的生成器。

🌐 Laravel 之外的示例

首先,使用顶部的类命名空间。

use Tzsk\Collage\MakeCollage;

$collage = new MakeCollage($driver); // Default: 'gd'

驱动器是 'gd' 或 'imagick'。根据您使用的 PHP 库,您可以自定义它。默认是 'gd'

使用 1 张图片创建拼贴

支持的,是的。

$image = $collage->make(400, 400)->from($images);

// Add Padding:
$image = $collage->make(400, 400)->padding(10)->from($images);

// Add Background Color:
$image = $collage->make(400, 400)->padding(10)->background('#000')->from($images);

使用 2 张图片创建拼贴

$image = $collage->make(400, 400)->from($images); // Default Alignment: vertical

// Change Alignment:
$image = $collage->make(400, 400)->from($images, function($alignment) {
    $alignment->vertical(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

使用 3 张图片创建拼贴

$image = $collage->make(400, 400)->from($images); // Default Alignment: twoTopOneBottom

// Change Alignment:
$image = $collage->make(400, 400)->from($images, function($alignment) {
    $alignment->twoTopOneBottom(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->oneTopTwoBottom();
    // OR...
    $alignment->oneLeftTwoRight();
    // OR...
    $alignment->twoLeftOneRight();
    // OR...
    $alignment->vertical();
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

使用 4 张图片创建拼贴

$image = $collage->make(400, 400)->from($images); // Default Alignment: grid

// Change Alignment:
$image = $collage->make(400, 400)->from($images, function($alignment) {
    $alignment->grid(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->vertical();
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

😍 Laravel 中的示例

在 Laravel 中,您已经有了拼贴制作器的别名

use Tzsk\Collage\Facade\Collage;

$image = Collage::make(400, 400)->from($images);

其余功能与在常规 PHP 中使用时相同。

使用 1 张图片创建拼贴

$image = Collage::make(400, 400)->from($images);

// Add Padding:
$image = Collage::make(400, 400)->padding(10)->from($images);

// Add Background Color:
$image = Collage::make(400, 400)->padding(10)->background('#000')->from($images);

使用 2 张图片创建拼贴

$image = Collage::make(400, 400)->from($images); // Default Alignment: vertical

// Change Alignment:
$image = Collage::make(400, 400)->from($images, function($alignment) {
    $alignment->vertical(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

使用 3 张图片创建拼贴

$image = Collage::make(400, 400)->from($images); // Default Alignment: twoTopOneBottom

// Change Alignment:
$image = Collage::make(400, 400)->from($images, function($alignment) {
    $alignment->twoTopOneBottom(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->oneTopTwoBottom();
    // OR...
    $alignment->oneLeftTwoRight();
    // OR...
    $alignment->twoLeftOneRight();
    // OR...
    $alignment->vertical();
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

使用 4 张图片创建拼贴

$image = Collage::make(400, 400)->from($images); // Default Alignment: grid

// Change Alignment:
$image = Collage::make(400, 400)->from($images, function($alignment) {
    $alignment->grid(); // Default, no need to have the Closure at all.
    // OR...
    $alignment->vertical();
    // OR...
    $alignment->horizontal();
});

您还可以在此处添加 padding()background()

🏆 返回值

返回的 $imageIntervention\Image\Image 对象的实例。

您可以对其进行多项操作。

  • 您可以保存最终的拼贴。
  • 您可以将其用作纯响应。
  • 您可以裁剪/调整大小/着色等。

有关您可以在其中做什么的更多信息,请参阅 官方文档

🔌 创建自定义生成器

创建生成器非常简单。创建一个扩展抽象类 Tzsk\Collage\Contracts\CollageGenerator 的类。

示例

use Tzsk\Collage\Contracts\CollageGenerator;

class FiveImage extends CollageGenerator
{
    /**
     * @param Closure $closure
     * @return \Intervention\Image\Image
     */
    public function create($closure = null)
    {
        // Your code to generate the Intervention\Image\Image object
    }
}

注意: 有关生成器类中可访问的所有内容的详细信息,请参阅 src/Contracts/CollageGenerator.php。如果您需要参考,请考虑查看 src/Generators/FourImage.php

在 Laravel 之外扩展

$image = $collage->with([5 => Your\Class\Namespace\FiveImage::class]);
// Here the key is the number of file your generator accepts.
// After this you can continue chaining methods like ->padding()->from() as usual.

您还可以覆盖现有生成器。假设您想要 FourImage 生成器有不同的行为。您可以创建自己的 MyFourImage 类并添加它。

$image = $collage->with([4 => Your\Class\Namespace\MyFourImage::class]);
// It will replace the existing Generator with your own.
// After this you can continue chaining methods like ->padding()->from() as usual.

在 Laravel 中扩展

$image = Collage::with([5 => Your\Class\Namespace\FiveImage::class]);
// Here the key is the number of file your generator accepts.
// After this you can continue chaining methods like ->padding()->from() as usual.

您还可以覆盖现有生成器。假设您想要 FourImage 生成器有不同的行为。您可以创建自己的 MyFourImage 类并添加它。

$image = Collage::with([4 => Your\Class\Namespace\MyFourImage::class]);
// It will replace the existing Generator with your own.
// After this you can continue chaining methods like ->padding()->from() as usual.

您还可以从 config/collage.php 配置文件中执行此操作。

存在一个当前为空的generators数组。您可以在其中添加自己的生成器,如下所示以替换或添加新的生成器。

'generators' => [
    // It will replace the current FourImage generator.
    4 => Your\Class\Namespace\MyFourImage::class,

    // It will add a new generator.
    5 => Your\Class\Namespace\FiveImage::class,
]

🔬 测试

composer test

📅 更新日志

请参阅更新日志获取关于最近更改的更多信息。

❤️ 贡献

请参阅贡献指南以获取详细信息。

🔒 安全性

如果您发现任何与安全性相关的问题,请通过电子邮件mailtokmahmed@gmail.com联系,而不是使用问题跟踪器。

👑 致谢

👮‍♂️ 许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。