lovell / lovell/sharp

How does sharp infer the height if the width is provided?

Open
#4,353 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
32.7k
Forks
1.4k
Avg merge
1d 14h
Merged PRs (30d)
5

Description

## Question about an existing feature

### What are you trying to achieve?
I am writing a webpack loader to optimize images, some of that work includes resizing. For dev purposes I need to know up-front what will be the image dimensions. As the user can specify the width only I need to infer the height. After looking at sharp source code I found these lines:
https://github.com/lovell/sharp/blob/7c631c0787915416e20a567a039516e99c81c42d/src/pipeline.cc#L176-L184

I thought that this must be it so I reimplemented this logic in my loader:
```js
// https://github.com/lovell/sharp/blob/7c631c0787915416e20a567a039516e99c81c42d/src/pipeline.cc#L176-L184
function inferHeight(currentWidth, currentHeight, newWidth) {
const xFactor = currentWidth / newWidth;
const newHeightNotRounded = currentHeight / xFactor;
const newHeight = Math.round(newHeightNotRounded);
return newHeight;
}
```

Unfortunately there must be more to it. For the target width of `496` and current height of `2731` and current width of `4096` this code inferred `331`. While sharp resized the image to `330`. It's not the rounding semantics and it's not the numerical precision, I check these already.

So the question stands, how does sharp auto-scale the images?

Best,
Wiktor

### Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question

I've created a smal example with the image that behaves in this way (other images work with the inference code). Running index.js in this repo, will run sharp and this inference function: [The link to a reproduction repo](https://github.com/watjurk/sharp-resize-inference)

### Please provide sample image(s) that help explain this question

[The sample image](https://github.com/watjurk/sharp-resize-inference/blob/master/image.jpeg)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/pipeline.cc at lines 176-184 and run index.js from the linked reproduction repository against image.jpeg. Compare the loader's inferred dimensions with sharp's output for width 496, current dimensions 4096×2731, and trace the relevant resize behavior. Done means explaining why sharp produces height 330 while the shown calculation produces 331.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.