我的表单中有两个输入图像,和一个registerMediaConversions:
public function registerMediaConversions(Media $media = null) : void
{
$this->addMediaConversion('big')
->performOnCollections('category-cover');
$this->addMediaConversion('medium')
->crop(Manipulations::CROP_CENTER, 645, 300)
->performOnCollections('category-cover');
$this->addMediaConversion('small')
->crop(Manipulations::CROP_CENTER, 300, 300)
->performOnCollections('category-cover');
$this->addMediaConversion('thumb')
->width(100)
->sharpen(10)
->performOnCollections('category-cover');
}
我想要第一个输入做大,中,拇指转换,第二个输入做小转换,这是可能的laravel媒体库吗?
是有可能的。要做到这一点,您必须将集合定义为与模型中的输入相同的集合。例如:
public function registerMediaCollections(): void
{
$this->addMediaCollection('category-cover');
$this->addMediaCollection('categories-another-collection');
}
那么您需要为2个不同的集合注册媒体转换,如下所示:
public function registerMediaConversions(Media $media = null) : void
{
$this->addMediaConversion('big')
->performOnCollections('category-cover');
$this->addMediaConversion('medium')
->crop(Manipulations::CROP_CENTER, 645, 300)
->performOnCollections('category-cover');
$this->addMediaConversion('small')
->crop(Manipulations::CROP_CENTER, 300, 300)
->performOnCollections('categories-another-collection');
$this->addMediaConversion('thumb')
->width(100)
->sharpen(10)
->performOnCollections('category-cover');
}
假设,在你的控制器中有2个字段请求,如下所示:
$validator = Validator::make($request->all(), [
'files1' => 'required',
'files1.*' => 'image|mimes:jpg,jpeg,png,gif|max:2048',
'files2' => 'required',
'files2.*' => 'image|mimes:jpg,jpeg,png,gif|max:2048',
]);
然后你可以保存这两种类型的文件:
if ($request->hasFile('files1')) {
$fileAdders = $categoryModel->addMultipleMediaFromRequest(['images'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('category-cover');
});
}
if ($request->hasFile('files2')) {
$fileAdders = $categoryModel->addMultipleMediaFromRequest(['images'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('categories-another-collection');
});
}
最后,files1输入请求将使用基于集合(category-cover)的大、中、小转换,而files2输入请求将只使用基于集合('categories-another-collection')的小转换