Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resize: withoutEnlargement: Height disregarded when cover && width > original width #2020

Closed
BrychanOdlum opened this issue Jan 2, 2020 · 3 comments

Comments

@BrychanOdlum
Copy link
Contributor

BrychanOdlum commented Jan 2, 2020

What are the steps to reproduce?

  1. Take an image of dimensions 1500 x 1000.

await Sharp.file(imageData)
  .resize({ width: 1750, height: 250, fit: 'cover', withoutEnlargement: true });

What is the expected behaviour?
An image of 1500 x 250.

Instead we get an image of 1500 x 1000.

Example code:
https://github.com/BrychanOdlum/Sharp-BugExample/blob/master/main.js
https://github.com/BrychanOdlum/Sharp-BugExample (has input & output image in repo)

What is the output of running npx envinfo --binaries --languages --system --utilities?
System:
OS: macOS 10.15
CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Memory: 232.27 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.11.0 - ~/.nvm/versions/node/v12.11.0/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.11.0/bin/npm
Utilities:
CMake: 3.15.4 - /usr/local/bin/cmake
Make: 3.81 - /usr/bin/make
GCC: 4.2.1 - /usr/bin/gcc
Git: 2.21.0 - /usr/bin/git
Clang: 1100.0.33.8 - /usr/bin/clang
Subversion: 1.12.2 - /usr/local/bin/svn
Languages:
Bash: 3.2.57 - /bin/bash
Go: 1.12.6 - /usr/local/go/bin/go
Java: 1.8.0_161 - /usr/bin/javac
Perl: 5.30.0 - /usr/local/bin/perl
PHP: 7.3.8 - /usr/bin/php
Python: 2.7.16 - /usr/local/bin/python
Python3: 3.7.5 - /usr/local/bin/python3
Ruby: 2.6.3 - /usr/bin/ruby

@BrychanOdlum
Copy link
Contributor Author

At the surface it looks as if this here is the culprit:

sharp/src/pipeline.cc

Lines 226 to 237 in 755a0ca

if (baton->withoutEnlargement) {
if (inputWidth < baton->width || inputHeight < baton->height) {
xfactor = 1.0;
yfactor = 1.0;
xshrink = 1;
yshrink = 1;
xresidual = 1.0;
yresidual = 1.0;
baton->width = inputWidth;
baton->height = inputHeight;
}
}

Might changing that to something like this instead do the trick?

      if (baton->withoutEnlargement) {
        if (inputWidth < baton->width) {
          xfactor = 1.0;
          xshrink = 1;
          xresidual = 1.0;
          baton->width = inputWidth;
        }

        if (inputHeight < baton->height) {
          yfactor = 1.0;
          yshrink = 1;
          yresidual = 1.0;
          baton->height = inputHeight;
        }
      }

@lovell
Copy link
Owner

lovell commented Jan 6, 2020

Hi Brychan, this is a nice bug, rather amazing it took this long for someone to find so thank you for reporting it!

I think the proposal above might impact the aspect ratio; the following alternative could also work:

- if (baton->withoutEnlargement) { 
+ if (baton->withoutEnlargement && baton->width > inputWidth && baton->height > inputHeight) {

Happy to accept a PR with a fix and your example scenario as a new test case to prevent this occurring again, if you're able.

@lovell lovell added bug and removed triage labels Jan 6, 2020
@lovell lovell added this to the v0.24.0 milestone Jan 6, 2020
@BrychanOdlum BrychanOdlum changed the title resize: withoutEnlargement & cover: Height disregarded when width > original width resize: withoutEnlargement: Height disregarded when width > original width Jan 6, 2020
@BrychanOdlum BrychanOdlum changed the title resize: withoutEnlargement: Height disregarded when width > original width resize: withoutEnlargement: Height disregarded when cover && width > original width Jan 6, 2020
@lovell
Copy link
Owner

lovell commented Jan 16, 2020

v0.24.0 is now available. Thanks for both reporting and fixing this!

@lovell lovell closed this as completed Jan 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants