Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency support with PNGs in conversions for spatie/media-library

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();
            });
    }
like image 897
Ehs4n Avatar asked Oct 19 '25 19:10

Ehs4n


1 Answers

You can add ->keepOriginalImageFormat() in registerMediaConversions

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

like image 136
ii iml0sto1 Avatar answered Oct 21 '25 09:10

ii iml0sto1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!