backpack/medialibrary-uploaders

使用spatie media library保存文件的辅助函数

1.2.0 2024-03-19 15:19 UTC

README

Latest Version on Packagist Total Downloads The Whole Fruit Manifesto

如果你的项目同时使用Spatie Media LibraryBackpack for Laravel,这个插件提供以下功能:

  • Backpack字段可以轻松地将上传的文件作为媒体存储(通过使用Spatie Media Library);
  • Backpack列可以轻松地检索上传的文件作为媒体;

更确切地说,它提供了->withMedia()辅助函数,该函数将处理文件上传和检索,使用Backpack Uploaders。你会爱上它使上传变得多么简单!

要求

安装和使用spatie/laravel-medialibrary v10。如果您还没有安装,请确保您已安装spatie/laravel-medialibrary并按照他们的文档中的所有安装步骤进行操作。

# require the package
composer require "spatie/laravel-medialibrary:^10.0.0"

# prepare the database
# NOTE: Spatie migration does not come with a `down()` method by default, add one now if you need it
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"

# run the migration
php artisan migrate

# make sure you have your storage symbolic links created for the default laravel `public` disk
php artisan storage:link

# (optionally) publish the config file
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

然后准备您的模型以使用spatie/laravel-medialibrary,通过将InteractsWithMedia特性添加到您的模型中,并实现HasMedia接口,如媒体库文档中所述。

安装

只需使用Composer要求此包即可

composer require backpack/medialibrary-uploaders

用法

在任何上传文件的字段上(例如uploadupload_multipleimagedropzone),在您的字段定义中添加withMedia(),以便告诉Backpack使用Spatie的Laravel MediaLibrary存储这些上传文件。例如

CRUD::field('avatar')->type('image')->withMedia();

// you can also do that on columns:
CRUD::column('avatar')->type('image')->withMedia();

// and on subfields:
CRUD::field('gallery')
        ->label('Image Gallery')
        ->type('repeatable')
        ->subfields([
            [
                'name' => 'main_image',
                'label' => 'Main Image',
                'type' => 'image',
                'withMedia' => true,
            ],
        ]); 

高级用法

覆盖默认值

当处理媒体时,Backpack为您设置了一些方便的默认值。但它也提供了一种方法来自定义您从Spatie Media Library中需要的东西。您可以将配置数组传递给->withMedia([])'withMedia' => []以覆盖Backpack设置的默认值。

CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'collection' => 'my_collection', // default: the spatie config default
            'disk' => 'my_disk', // default: the spatie config default
            'mediaName' => 'custom_media_name' // default: the field name
        ]);

自定义保存过程(添加缩略图、响应式图像等)

在上面的同一配置数组中,您可以使用whenSaving闭包来自定义保存过程。此闭包将在配置媒体集合的过程中被调用。因此,在调用初始化函数之后,但在调用toMediaCollection()之前。使用Spatie的文档中记录的方法对$spatieMedia对象执行您想要的操作,然后使用return将其返回给Backpack以调用终止方法。听起来不错吗?

CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'whenSaving' => function($spatieMedia, $backpackMediaObject) {
                return $spatieMedia->usingFileName('main_image.jpg')
                                    ->withResponsiveImages();
            }
        ]);

注意:某些方法将由Backpack自动调用;您不应在用于配置的闭包中调用它们:toMediaCollection()setName()usingName()setOrder()toMediaCollectionFromRemote()toMediaLibrary()。如果您在闭包中手动尝试调用它们,则将引发错误。

在模型中定义媒体集合

您还可以按照Spatie文档中的说明在模型中配置集合,在这种情况下,您只需传递collection配置键。但您仍然可以配置所有其他选项,包括whenSaving回调。

// In your Model.php

public function registerMediaCollections(): void
{
    $this
        ->addMediaCollection('product_images')
        ->useDisk('products');
}

// And in YourCrudController.php
CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'collection' => 'product_images', // will pick the collection definition from your model
        ]);

处理转换

有时您可能需要为您的图片创建转换,例如缩略图等。如果您想在字段上显示转换而不是原始图片,应定义 displayConversions => 'conversion_name'displayConversions => ['higher_priority_conversion', 'second_priority_conversion']

最后,如果没有任何转换准备就绪(可能它们仍在排队),我们将显示原始文件作为备用。

// In your Model.php

public function registerMediaConversions(): void
{
    $this->addMediaConversion('thumb')
                ->width(368)
                ->height(232)
                ->keepOriginalImageFormat()
                ->nonQueued();
}

// And in YourCrudController.php
CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'displayConversions' => 'thumb'
        ]);
        
// you can also configure aditional manipulations in the `whenSaving` callback
->withMedia([
    'displayConversions' => 'thumb',
    'whenSaving' => function($media) {
        return $media->withManipulations([
            'thumb' => ['orientation' => 90]
        ]);
    }
]);

自定义属性

您可以使用 ->withCustomProperties([]) 将自定义属性分配给您的媒体,如 spatie 文档所述,但请注意,namerepeatableContainerNamerepeatableRow保留关键字,Backpack 值将 始终覆盖您的值。

'whenSaving' => function($media) {
        return $media->withCustomProperties([
            'my_property' => 'value',
            'name' => 'i_cant_use_this_key'
        ]);
    }

// the saved custom properties will be: 
//  - [my_property => value, name => main_image, repeatableRow => null, repeatableContainerName => null]`

变更日志

更改记录在 Github 上。请参阅 版本标签

测试

composer test

贡献

有关待办事项列表和如何操作,请参阅 contributing.md

安全

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

致谢

许可

本项目基于 MIT 许可发布,因此您可以在任何 Backpack & Laravel 项目上安装它。有关更多信息,请参阅 许可文件