marshmallow / advanced-nova-media-library
Laravel Nova 用于管理 Spatie 媒体库的工具。
Requires
- php: ^8.0|^8.1
- laravel/framework: ^9.0|^10.0|^11.0
- laravel/nova: ^4.0
- spatie/laravel-medialibrary: ^10.0|^11.0
- dev-master
- v4.5.2
- v4.5.1
- v4.5.0
- v4.4.0
- v4.3.0
- v4.2.1
- v4.2.0
- v4.1.1
- v4.1.0
- 4.0.2
- 4.0.1
- v4.0.0
- v3.8.1
- 3.8.0
- v3.7.2
- v3.7.1
- v3.7.0
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.0
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.2.1
- 0.2.0
- 0.1.3
- 0.1.2
- 0.1.1
- dev-version-4
- dev-dependabot/npm_and_yarn/follow-redirects-1.14.7
- dev-dependabot/npm_and_yarn/url-parse-1.5.3
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dev
This package is auto-updated.
Last update: 2024-09-26 13:21:40 UTC
README
管理 spatie 的媒体库包 中的图片。可以通过拖放上传多个图片并排序。
目录
示例
安装
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): void { $this->addMediaConversion('thumb') ->width(130) ->height(130); } public function registerMediaCollections(): void { $this->addMediaCollection('main')->singleFile(); $this->addMediaCollection('my_multi_collection'); }
通用文件管理
为了能够上传和处理通用文件,只需使用 Files
字段即可。
use Ebess\AdvancedNovaMediaLibrary\Fields\Files; Files::make('Single file', 'one_file'), Files::make('Multiple files', 'multiple_files'),
单个图片上传
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 ]; }
多个图片上传
如果您启用了多文件上传功能,则可以通过 拖放 对图片进行排序。
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'), ]; }
选择现有媒体
如果您将相同的媒体文件上传到多个模型,并且不想再次从文件系统中选择,请使用此功能。选择已存在的媒体将会 复制 它。
注意: 此功能将向您的应用程序的每个用户公开搜索现有媒体。如果您的媒体上传/媒体模型的自定义属性是机密的,请 不要启用此功能!
- 如果尚未发布,请发布配置文件
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();
图片裁剪
默认情况下,您可以通过在编辑视图的左下角点击剪刀来裁剪/旋转图片。用于此目的的是 vue-js-clipper。裁剪功能仅限于 image/jpg
、image/jpeg
和 image/png
的 MIME 类型。
重要: 通过裁剪现有图片,原始媒体模型将被删除并替换为裁剪后的图片。所有自定义属性将从旧模型复制到新模型。
要禁用此功能,请使用 croppable
方法
Images::make('Gallery')->croppable(false);
您可以像以下这样设置所有配置,例如比例
Images::make('Gallery')->croppingConfigs(['aspectRatio' => 4/3]);
可用的裁剪配置,请参阅 https://github.com/timtnleeProject/vuejs-clipper#clipper-basic。
在上传时可以强制执行裁剪,例如确保图片具有设置的宽高比。
Images::make('Gallery')->mustCrop();
默认禁用裁剪。
默认情况下,裁剪功能是启用的。要为所有图片默认禁用裁剪,请在 config/nova-media-library.php
中将 default-croppable
设置为 false
return [ 'default-croppable' => false, ];
自定义属性
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(), ]);
显示图片统计信息 (大小,尺寸,类型)
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): void { $this->addMediaConversion('thumb') ->width(368) ->height(232) ->extractVideoFrameAtSecond(1); } }
临时网址
如果您使用 Amazon S3 存储您的媒体,您需要使用字段的 temporary
函数来生成临时签名网址。此函数需要一个有效的 Carbon 实例,该实例将指定网址何时过期。
Images::make('Image 1', 'img1')
->temporary(now()->addMinutes(5))
Files::make('Multiple files', 'multiple_files')
->temporary(now()->addMinutes(10),
注意:此功能与现有媒体功能不兼容。
致谢
替代方案
变更日志
v4.0.2 - 2022-04-26
- 在 Nova 4 中修复裁剪比例。现在将从
Images::( ... )>croppingConfigs()
传递配置到裁剪器的stencil-props
属性。有关可用的属性详细信息,请参阅 裁剪器文档。
v4.0.1 - 2022-04-20
- 修复详细组件
- 修复布局不一致问题
v4.0.0 - 2022-04-18
- 升级以支持 Laravel Nova 4
- 与 Laravel Nova 1、2 和 3 不兼容。对于这些 nova 版本,请使用
v3.*
- 将 vuejs-clipper 替换为 vue-advanced-cropper 以支持 vue3
完整的变更日志请见 PR #317