[SOLVED] Assets "Crop" mode does not behave as expected for small images

I’m submitting a…

[ ] Regression (a behavior that stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Documentation issue or request

Current behavior

When using “Crop” resize mode for images assets and the actual image is smaller than requested size then resize behavior is reset to “BoxPad”.

For example, I have 600x400 images and want to crop/resize it to:

  • 320x320
  • 640x640

In first case the image will be correctly resized and cropped, but in second case the image is not resized at all but it is pad with black color instead.

Expected behavior

Even if the image is smaller than target size I expect it to be scaled up and cropped, instead of padding it with black.

E.g. “600x400” image (if targeting “640x640”) should be scaled up to “960x640” and then cropped to “640x640”.

Alternatively we can avoid scaling images up by adjusting target size, e.g. for “600x400” image (if targeting “640x640”) we should adjust target size to “400x400” instead and return 400x400 images cropped out from original image.

Note, that in our case images are uploaded by customers, so we can’t control their sizes and we don’t want to restrict minimum images sizes. It will be good if Crop mode is implemented correctly.

Minimal reproduction of the problem

You can experiment with this image: https://www.w3schools.com/w3css/img_lights.jpg, and use “640x640” as target size, e.g. “[asset_url]?width=640&height=640&mode=Crop”.

Environment

  • [ ] Self hosted with docker
  • [x] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Browser:

  • [ ] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Others:

BTW, I think there is a typo in Assets docs (https://docs.squidex.io/guides/05-assets), the last mode should be “Stretch”, not “Crop” (already defined above).

I see that it is done intentionally in code:

                if (w >= sourceImage.Width && h >= sourceImage.Height && resizeMode == ResizeMode.Crop)
                {
                    resizeMode = ResizeMode.BoxPad;
                }

But I don’t really think it is a correct behavior if “Crop” mode was requested.

Thank you. I have to think about the reason, I don’t remember why I did this.

I don’t want to change this behavior, but we could introduce a new mode called “CropUpsize” or so which has the correct behavior.

What’s the reason to keep “Crop” logic like this? Do you think somebody can rely on this behavior already?

Anyway, introducing “CropUpsize” mode should help too, thanks.

I think I have changed it for the UI. When you upload a small icon it becomes very ugly in the asset list.

But I am not sure who else relies on it, this is the problem.

Hi, I have pushed and deployed a fix.

Hi, thanks, works just fine!

1 Like