question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to get the size of an image after resizing?

See original GitHub issue

Does Sharp offer a simple way to get the size of an image after it has been resized?

Here’s roughly what I’m trying to accomplish:

const image = sharp(filepath);
// do other stuff with `image`
const thumbnail = image.clone().resize({height: THUMBNAIL_HEIGHT, fit: "inside"});

I’m using the inside fit option to automatically determine the correct width for the resized image. I thought Sharp#metadata() might be useful but then I read this line in the docs:

It does not include operations, such as resize, to be applied to the output image.

Does Sharp support what I am trying to accomplish?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pjhartincommented, Dec 12, 2022

Thanks @rawpixel-vincent, I adapted your example with an async variant.

const { info, data } = await sharp(fileOrBuffer)
    .withMetadata()
    .resize({
      width: 3000,
      height: 3000,
      withoutEnlargement: true,
      fit: 'inside'
    })
    .jpeg({
      quality: 80
    })
    .toBuffer({ resolveWithObject: true })

The size can then be accessed at info.size and the buffer is available at data.buffer.

Hope this helps someone else.

1reaction
lovellcommented, Jun 16, 2022

@Enchiridion The call to toBuffer() (or toFile, or pipe) triggers the pipeline and starts the calculations. If you need the output dimensions before the output is generated then you’ll have to calculate these in your own code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get actual image size, after resizing it - javascript
function getImageDimensions(path,callback ){ var img = new Image(); img.onload = function(){ callback({ width : img.width, height : img.height }); ...
Read more >
How to resize your images quickly and easily | ZDNET
Locate the W x H x Resolution menu at the top of the screen. Then, click on the left drop-down and click W...
Read more >
How to Resize and Make Images Larger without Losing Quality
Open the file in Photshop, click on “image size, un-check “Resample image”, then start adjusting the pixel size under width until you reach...
Read more >
How to Resize an Image [from Any Device] - Alphr
1. Open one of your favorite browsers and visit TinyWow.com. 2. Hover your mouse to the Image tab and click Resize Image Dimension...
Read more >
How to Resize an Image | Digital Trends
1. Right-click on the image and select Open With followed by Photos. 2. Click the Three-Dot button located in the app’s top-right corner....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found