chasie/advanced-nova-media-library

Laravel Nova 管理Spatie媒体库的工具。

安装: 17

依赖: 0

建议者: 0

安全: 0

星级: 0

关注者: 0

分支: 295

语言:Vue

3.6.3 2021-11-18 12:32 UTC

README

管理 spatie的媒体库包 的图像。上传多张图像并通过拖放排序。

目录

示例

Cropping Single image upload Multiple image upload Custom properties Generic file management

安装

composer require ebess/advanced-nova-media-library
php artisan vendor:publish --tag=nova-media-library

模型媒体配置

假设您已配置模型使用媒体库,如下所示

use Spatie\MediaLibrary\MediaCollections\Models\Media;

public function registerMediaConversions(Media $media = null)
{
    $this->addMediaConversion('thumb')
        ->width(130)
        ->height(130);
}

public function registerMediaCollections()
{
    $this->addMediaCollection('main')->singleFile();
    $this->addMediaCollection('my_multi_collection');
}

通用文件管理

Generic file management

为了能够上传和处理通用文件,请使用 Files 字段。

use Ebess\AdvancedNovaMediaLibrary\Fields\Files;

Files::make('Single file', 'one_file'),
Files::make('Multiple files', 'multiple_files'),

单张图像上传

Single image upload

use Ebess\AdvancedNovaMediaLibrary\Fields\Images;

public function fields(Request $request)
{
    return [
        Images::make('Main image', 'main') // second parameter is the media collection name
            ->conversionOnIndexView('thumb') // conversion used to display the image
            ->rules('required'), // validation rules
    ];
}

多张图像上传

如果您启用了多文件上传功能,您可以通过 拖放 来排序图像。

Multiple image upload

use Ebess\AdvancedNovaMediaLibrary\Fields\Images;

public function fields(Request $request)
{
    return [
        Images::make('Images', 'my_multi_collection') // second parameter is the media collection name
            ->conversionOnPreview('medium-size') // conversion used to display the "original" image
            ->conversionOnDetailView('thumb') // conversion used on the model's view
            ->conversionOnIndexView('thumb') // conversion used to display the image on the model's index page
            ->conversionOnForm('thumb') // conversion used to display the image on the model's form
            ->fullSize() // full size column
            ->rules('required', 'size:3') // validation rules for the collection of images
            // validation rules for the collection of images
            ->singleImageRules('dimensions:min_width=100'),
    ];
}

选择现有媒体

Selecting existing media Selecting existing media 2

如果您将相同的媒体文件上传到多个模型,并且不想再次从文件系统中选择,请使用此功能。选择已存在的媒体将 复制它

注意:此功能将向您的应用程序的每个用户公开一个搜索现有媒体端点。如果您的媒体上传/媒体模型上的自定义属性是机密的,请勿启用此功能!

  • 如果您尚未发布配置文件,请发布它们
artisan vendor:publish --tag=nova-media-library
  • 在配置文件 config/nova-media-library 中启用此功能
return [
    'enable-existing-media' => true,
];
  • 启用现有媒体字段的选取
Images::make('Image')->enableExistingMedia(),

注意:此功能不支持临时URL。

上传图像的名称

新上传文件的默认文件名为原始文件名。您可以使用 setFileName 函数更改此名称,该函数只接受一个回调函数作为参数。此回调函数有三个参数:$originalFilename(原始文件名,如 Fotolia 4711.jpg)、$extension(文件扩展名,如 jpg)和 $model(当前模型)。以下仅是您能做的两个示例

// Set the filename to the MD5 Hash of original filename
Images::make('Image 1', 'img1')
    ->setFileName(function($originalFilename, $extension, $model){
        return md5($originalFilename) . '.' . $extension;
    });

// Set the filename to the model name
Images::make('Image 2', 'img2')
    ->setFileName(function($originalFilename, $extension, $model){
        return str_slug($model->name) . '.' . $extension;
    });

默认情况下,媒体对象的 "name" 字段设置为不带扩展名的原始文件名。要更改此设置,您可以使用 setName 函数。与 setFileName 类似,它只接受一个回调函数作为参数。此回调函数有两个参数:$originalFilename$model

Images::make('Image 1', 'img1')
    ->setName(function($originalFilename, $model){
        return md5($originalFilename);
    });

响应式图像

如果您想使用来自 Spatie MediaLibrary 的响应式图像功能,您可以在模型上使用 withResponsiveImages() 函数。

Images::make('Image 1', 'img1')
    ->withResponsiveImages();

图像裁剪

Cropping

默认情况下,您可以通过在编辑视图的左下角点击剪刀来裁剪/旋转图像。此操作使用 vue-js-clipper 实现。裁剪功能仅限于 image/jpgimage/jpegimage/png 的 MIME 类型。

重要:通过裁剪现有图像,原始媒体模型将被删除并由裁剪图像替换。所有自定义属性都将从旧模型复制到新模型。

要禁用此功能,请使用 croppable 方法

Images::make('Gallery')->croppable(false);

您可以将所有配置(如比例等)设置如下

Images::make('Gallery')->croppingConfigs(['ratio' => 4/3]);

可用的裁剪配置,请参阅https://github.com/timtnleeProject/vuejs-clipper#clipper-basic

可以在上传时强制执行裁剪,例如确保图像具有设置的宽高比

Images::make('Gallery')->mustCrop();

自定义属性

Custom properties

Images::make('Gallery')
    ->customPropertiesFields([
        Boolean::make('Active'),
        Markdown::make('Description'),
    ]);
    
Files::make('Multiple files', 'multiple_files')
    ->customPropertiesFields([
        Boolean::make('Active'),
        Markdown::make('Description'),
    ]);
    
// custom properties without user input
Files::make('Multiple files', 'multiple_files')
    ->customProperties([
        'foo' => auth()->user()->foo,
        'bar' => $api->getNeededData(),
    ]);

显示图像统计信息(大小、尺寸、类型)

Image statistics

Images::make('Gallery')
    ->showStatistics();

自定义头部

Images::make('Gallery')
    ->customHeaders([
        'header-name' => 'header-value', 
    ]);

媒体字段(视频)

为了处理带有缩略图的视频,您需要使用Media字段而不是Images字段。这样,您就可以上传视频了。

use Ebess\AdvancedNovaMediaLibrary\Fields\Media;

class Category extends Resource
{
    public function fields(Request $request)
    {
        Media::make('Gallery') // media handles videos
            ->conversionOnIndexView('thumb')
            ->singleMediaRules('max:5000'); // max 5000kb
    }
}

// ..

class YourModel extends Model implements HasMedia
{
    public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb')
            ->width(368)
            ->height(232)
            ->extractVideoFrameAtSecond(1);
    }
}

临时URL

如果您使用Amazon S3存储媒体,您需要使用字段上的temporary函数来生成临时签名URL。此函数期望一个有效的Carbon实例,该实例将指定URL何时应过期。

Images::make('Image 1', 'img1')
    ->temporary(now()->addMinutes(5))

Files::make('Multiple files', 'multiple_files')
    ->temporary(now()->addMinutes(10),

注意:此功能与现有媒体功能不兼容。

致谢

替代方案