spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

fix png transparent bug #3474

Closed ariaieboy closed 11 months ago

ariaieboy commented 11 months ago

There is an unnecessary format change when doing conversation that makes some weird bug when we use the GD driver. This one line of code made me read the whole system of the image, image-optimizer, and laravel media library package 😊

the image input: image

the result after converting to webp format with a white background before this PR: image

and after this PR: image

freekmurze commented 11 months ago

By default, the media library will make jpg conversions. If you don't want that behaviour, tack on shouldKeepOriginalImageFormat() to your conversion and the result will be a png (when your input is a png)

ariaieboy commented 11 months ago

By default, the media library will make jpg conversions. If you don't want that behaviour, tack on shouldKeepOriginalImageFormat() to your conversion and the result will be a png (when your input is a png)

The keepOriginalImageFormat method is not fixing my problem. The problem is that when you use the imagick driver the format change won't take place until you write the image. But in the GD driver when you call the format method the image actually changes to that format. Since the JPG does not support transparent background. it's either gonna generate a black background or in my case this weird background.

ariaieboy commented 11 months ago

I think instead of changing this here we should change the GD driver format method. It must save the format and apply it when we call the save method.

freekmurze commented 11 months ago

I think instead of changing this here we should change the GD driver format method. It must save the format and apply it when we call the save method.

Yeah, that would be better I think. You're up for making that PR?