2

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();
            });
    }
apokryfos
  • 38,771
  • 9
  • 70
  • 114
Ehs4n
  • 762
  • 1
  • 8
  • 24

1 Answers1

2

You can add ->keepOriginalImageFormat() in registerMediaConversions

So when you upload a transparent .png file, it will keep the transparent background

ii iml0sto1
  • 1,654
  • 19
  • 37