spatie / laravel-medialibrary

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

Fit::Max resizing images to fill dimensions and painting background #3518

Closed rossbearman closed 6 months ago

rossbearman commented 10 months ago

The description of Fit::Max is:

Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio, and will also not increase the size of the image if it is smaller than the output size.

This was the case in v2, but it appears to have changed to instead expand the image if smaller (maintaining the aspect raio), and paint in a background colour if the image doesn't match the aspect ratio of the specified height/width.

If you pass an image with dimensions smaller than 1920x1920 to ->fit(Fit::Max, 1920, 1920) it will output a 1920x1920 image, with the original image scaled to fit the largest dimension and a white background on the other dimension. This is the behaviour with both the GD and Imagick drivers.

In v2 this would output the same size image as the input. The new behaviour is the same as Manipulations::FIT_FILL_MAX in v2.

The test for Fit::Max is titled "can fill and stretch an image in the given dimensions" and the reference test images display the same behaviour, suggesting this is now the intended behaviour, despite it deviating from the behaviour in v2.

The Glide implementation used in v2 applies the upsizing constraint to max, but Constraint::DoNotUpsize isn't applied in Spatie\Image\Enums\Fit in v3. Naively applying this constraint in v3 causes the image to not be upscaled, but it still expands the canvas to match whatever dimensions were passed in and paints in a background.

(ref https://github.com/spatie/image/discussions/233)

timvandijck commented 6 months ago

Merged the related PR: https://github.com/spatie/image/pull/239