ziffmedia/laravel-eloquent-imagery

针对 Laravel Eloquent 的按列/属性处理图片

v2.0.0-rc.2 2024-09-06 23:00 UTC

README

描述

ziffmedia/laravel-eloquent-imagery 以独特的方式处理图片。它将图片视为模型属性,而不是模型关系。因此,图片信息存储在特定名称的表列(模型属性)中。此外,此库在提供图片时而不是在上传时处理图片修改(调整大小、裁剪、背景)。图片存储在 Laravel 支持的任何配置的文件存储中。

特性

  • 图片在相同表的列中跟踪
  • 通过为每个图片类型添加一个特性和一个方法来处理设置
  • 可以通过操作图片的 URL 来进行修改
  • 支持占位符生成
  • 当图片不在磁盘上时,支持回退到占位符(主要针对开发目的)

其他优秀的库

如果您对解决图片问题的此方案不感兴趣,Spatie 的 MediaLibrary 是一个出色的库,它将图片视为模型关系,并在上传时具有“转换”概念。

安装

首先,安装包

$ composer require ziffmedia/laravel-eloquent-imagery

在 Laravel 5.5+ 中,此库将自动注册。接下来,您应该发布供应商(配置)文件

$ artisan vendor:publish --provider="ZiffMedia\LaravelEloquentImagery\EloquentImageryProvider"

现在可以使用它了。

用法

将图片附加到模型

在最简单的使用场景中,将单个图片附加到模型,首先在迁移中创建一个 json 列来处理此图片

// in a table migration
$table->json('image')->nullable();

接下来,在模型中添加 HasEloquentImagery 特性,并配置一个名为 $eloquentImagery 的类属性,如下所示

use ZiffMedia\LaravelEloquentImagery\Eloquent\HasEloquentImagery;

class Post extends Model
{
    use HasEloquentImagery;

    protected $eloquentImagery = [
        'path' => 'posts/{id}.{extension}',
    ];
}

将图片集合附加到模型

图片集合是 Image 对象的有序列表。当集合被填充时,它包含从 0 开始可索引的图片。每个集合都有一个概念性的 自动递增 数字,它存储在集合(以及集合的序列化)中,以便 Image 对象可以利用这一点在路径模板中。

在最简单的使用场景中,使用与上面直接图片场景相同的 json 列,如之前一样添加 HasEloquentImagery 特性,并在构造时使用 eloquentImageryCollection 方法来设置集合

use ZiffMedia\LaravelEloquentImagery\Eloquent\HasEloquentImagery;

class Post extends Model
{
    use HasEloquentImagery;

    protected $eloquentImagery = [
        'images' => [
            'path' => 'post/{id}/image-{index}.{extension}',
            'collection' => true
        ],
    ];
}

在视图中显示图片

以下 blade 语法假设 $post 是具有 image 属性的 Post 类型的模型。这将生成类似于 /imagery/post/11/image.png 的 URL

@if($bam->image->exists)
    <img src="{{ $bam->image->url() }}" width="20" />
@endif

在使用修饰符生成 URL 时,生成的 URL 类似于 /imagery/post/11/image.@size200x200@trim.png

@if($bam->image->exists)
    <img src="{{ $bam->image->url('size200x200|trim) }}" width="20" />
@endif

图片修饰符

TODO

详细配置

以下表格描述了可用的配置选项

eloquent-imagery.filesystem

默认值: env('IMAGERY_FILESYSTEM', 'public')

这是图片将在其路径上存储的文件系统。

eloquent-imagery.render.enable

默认值: true

是否启用渲染 路由 和修改功能。

eloquent-imagery.render.path

默认值: /imagery

路由将生活在其上的路径前缀,并从该路径提供图片。

eloquent-imagery.render.placeholder.enable

默认值: env('IMAGERY_RENDER_PLACEHOLDER_ENABLE', false)

非常有用,考虑在本地启用。

eloquent-imagery.render.placeholder.filename

默认值 _placeholder_

这标识当请求占位符图片时。

eloquent-imagery.render.placeholder.use_for_missing_files

默认值 env('IMAGERY_RENDER_PLACEHOLDER_USE_FOR_MISSING_FILES', false)

如果请求的图片不在文件系统中,启用此功能将服务占位符而不是图片(对开发很有用)。

eloquent-imagery.render.caching.enable

默认值 env('IMAGERY_RENDER_CACHING_ENABLE', true)

控制器是否应使用完整请求缓存。

eloquent-imagery.render.caching.driver

默认值 env('IMAGERY_RENDER_CACHING_DRIVER', 'disk')

将缓存存储在磁盘上。

eloquent-imagery.render.caching.ttl

默认值 60

缓存的有效期。

eloquent-imagery.render.browser_cache_max_age

默认值 31536000

浏览器应缓存由此路由生成的图片的时间。

eloquent-imagery.force_unmodified_image_rendering

默认值 env('IMAGERY_FORCE_UNMODIFIED_IMAGE_RENDERING', false)

这将允许根据图像请求中是否存在修饰符,选择性地使用动态(控制器)路由或静态路由(例如,链接到存储)。

演示

一旦克隆,进入 cd demo 目录。在里面执行以下操作:

composer install
artisan migrate
artisan db:seed
artisan serve

如果您想演示Nova特定的功能,您必须首先将nova的可下载版本安装到 demo/nova。安装完成后,继续从上面的脚本开始,然后在浏览器中访问 /nova 的示例。

TODO

  • 支持因路径部分更新(属性更新等)而移动图像。

更新

更新到 0.5.0

  • 请确保将 config/eloquent_imagery.php 重命名为 config/eloquent-imagery.php,可能最好重新复制原始文件(或再次发布)。
  • 查看配置模型以使用图像的新方法:使用名为 $eloqentImagery 的属性。