[英]spatie/laravel-medialibrary - Ability to add conversion per media model "on the fly"?
I'm using popular package spatie/laravel-medialibrary for associating files with models.我正在使用流行的 package spatie/laravel-medialibrary 将文件与模型相关联。
I was wondering if there is possibility add conversions on the fly, right before adding media to model.我想知道是否有可能在将媒体添加到 model 之前即时添加转换。
I tried something like this, but it seems like conversions are being ignored if they are added this way.我尝试过类似的方法,但如果以这种方式添加转换,则转换似乎会被忽略。
// $this being the model with HasMedia interface and InteractsWithMedia trait
use Spatie\MediaLibrary\Conversions\Conversion;
$this->mediaConversions = [
Conversion::create('name')
->withResponsiveImages()
->performOnCollections('default')
->format('webp'),
Conversion::create('another-one')
->withResponsiveImages()
->performOnCollections('default')
->format('webp'),
];
$this->addMedia($filePath)->toMediaCollection();
Is this somehow possible to do?这有可能做到吗?
Something like this would be nice:这样的事情会很好:
$model->addMedia($path)->withConversions([
Conversion::create('another-one')
->withResponsiveImages()
->performOnCollections('default')
->format('webp'),
])
But withConversions
doesn't exist in v10但是 v10 中不存在withConversions
Thank you for answering.谢谢你的回答。
You can register the image conversion directly in the model as described in the documentation here .您可以按照此处文档中的说明直接在 model 中注册图像转换。
To generate that thumbnail, you must add a conversion like this one to your model.要生成该缩略图,您必须将像这样的转换添加到您的 model。
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
public function registerMediaConversions(Media $media = null): void
{
$this
->addMediaConversion('preview')
->fit(Manipulations::FIT_CROP, 300, 300)
->nonQueued();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.