I am using spatie/media-library with my Laravel 8 project. In the model, I am writing a method to register a mediaCollection for a model. The issue I am facing is that during the conversion, the transparency is lost in the converted image.
How can I resize the image without losing transparency in result image?
Here is the code that I am using
public function registerMediaCollections(): void
{
$this->addMediaCollection('crops')
->singleFile()
->useDisk('public')
->acceptsMimeTypes([
'image/jpeg', 'image/png', 'image/jpg',
])
->onlyKeepLatest(1)
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
->fit(Manipulations::FIT_CROP, 150, 150)
->keepOriginalImageFormat();
});
}
You can add ->keepOriginalImageFormat() in registerMediaConversions
So when you upload a transparent .png file, it will keep the transparent background
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With