sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Problem with fit: 'cover + withoutEnlargement: true when image is smaller

Open igorescobar opened this issue 6 years ago • 7 comments

What is the output of running npx envinfo --binaries --languages --system --utilities?

Sharp: 0.22.1

System:
    OS: Linux 4.9 Amazon Linux AMI 2018.03
    CPU: (4) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 7.32 GB / 9.74 GB
    Container: Yes
    Shell: 4.2.46 - /bin/bash
  Binaries:
    Node: 8.16.0 - /usr/bin/node
    npm: 6.9.0 - /usr/bin/npm
  Utilities:
    CMake: 2.8.12.2 - /usr/bin/cmake
    Make: 3.82 - /usr/bin/make
    GCC: 4.8.5 - /usr/bin/gcc
    Git: 2.14.5 - /usr/bin/git
  Languages:
    Bash: 4.2.46 - /bin/bash
    Perl: 5.16.3 - /usr/bin/perl
    Python: 2.7.16 - /usr/bin/python

What are the steps to reproduce?

image.resize({
  width,
  height,
  fit: 'cover',
  position: sharp.gravity.center,
  withoutEnlargement: true
});

When image is bigger than width and height the crop it works. But when image is smaller the expected width and height the resulting image is the original image without any kind of resizing or crop.

Is this expected?

igorescobar avatar Jul 25 '19 10:07 igorescobar

Hi, you've set withoutEnlargement to be true so this is the expected behaviour.

https://sharp.pixelplumbing.com/en/stable/api-resize/#resize

options.withoutEnlargement Boolean do not enlarge if the width or height are already less than the specified dimensions, equivalent to GraphicsMagick's > geometry option. (optional, default false)

lovell avatar Jul 25 '19 11:07 lovell

Even the crop shouldn't happen?

igorescobar avatar Jul 25 '19 11:07 igorescobar

I was expecting an image at least smaller than width or height in that case.

igorescobar avatar Jul 25 '19 11:07 igorescobar

Example, this image: http://www.igorescobar.com/blog/wp-content/uploads/2014/08/cropped-golden-bg.jpeg (1000x287)

If I want a 300x200 image the output is: image

If I want a 300x300 image the output is: image

So this stops the entire crop operation itself.

igorescobar avatar Jul 25 '19 12:07 igorescobar

My expectation would be an image with 300x287px

igorescobar avatar Jul 25 '19 12:07 igorescobar

Or... a 300x300 image with the image centralised inside of it.

igorescobar avatar Jul 25 '19 12:07 igorescobar

I guess this is where trying to mimic both CSS' object-fit and *magick's command line flags at the same time fails.

Happy to accept a PR with test cases to address this if you're able. This might need to wait until v0.24.0 as it could be considered a breaking change.

lovell avatar Jul 29 '19 21:07 lovell

Closing due to inactivity but please feel free to reopen with more details if further help is required.

lovell avatar Mar 22 '23 07:03 lovell