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.

Resize image multiple times

See original GitHub issue

I had a look through the docs/wiki and couldn’t see any examples of what I’m trying to do, which is save multiple sizes of the same image after cropping:

sharp( imgBuffer )
    .resize(800, 800)
    .crop(sharp.gravity.centre)
    .toFile( imgLarge )
    .resize(300, 300)
    .toFile( imgMedium )
    .resize(100, 100)
    .toFile( imgSmall );

Would something like that work?

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
lovellcommented, Jan 27, 2015

Hi Jonathan,

An undocumented feature is that if the input is a filename, you can do exactly what you’re trying to achieve.

You’re using a Buffer, which behaves slightly differently to work around a nuance of the V8 garbage collector.

An input Buffer containing compressed image data is copied and unreferenced as soon as the first call to toFile (or toBuffer or pipe) is made. This is to avoid the situation where, should the memory compaction stage of the V8 garbage collector run in the time between a resize task being queued and being processed, the Buffer could move without us knowing.

In your example this means the internal reference to imgBuffer held by the instance of sharp doesn’t exist by the second call to toFile.

To provide the API you’re expecting, we’d need to change this module to hold two copies of the input Buffer in memory at the same time, one in JavaScript land and one in C++ land. The newer version of V8 and the changes to Buffer allocation in Node.js 0.11 unstable (and io.js) might improve the situation, so I’d be happy to revisit this when they stabilise.

As an aside, the default behaviour is to crop with centre gravity so you can leave the .crop(sharp.gravity.centre) bit out.

1reaction
nmeccommented, Feb 12, 2015

Not in a single chain like I outlined above, instead I keep the image buffer in memory and loop over it for each size I need:

var sizes = [{
    path: 'large',
    xy: 800
},{
    path: 'medium',
    xy: 300
},{
    path: 'small',
    xy: 100
}];

Promise.map(sizes, function(size) {
    return sharp( img.buffer )
        .resize( size.xy, size.xy )
        .toFile( size.path );
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bulk Image Resizer - Edit Multiple Images Online - RedKetchup
Browse and select multiple images you want to resize, convert, or compress in batch. Drag-and-drop the image files or image folders.
Read more >
Resize multiple images at once! - iLoveIMG
Resize multiple JPG, PNG, SVG or GIF images in seconds easily and for free. Bulk resize images by defining pixels or percentages.
Read more >
How to Quickly Resize Multiple Images on Windows 10
Select a group of images with your mouse, then right-click them. In the menu that pops up, select “Resize pictures.” An Image Resizer...
Read more >
BIRME - Bulk Image Resizing Made Easy 2.0 (Online & Free)
BIRME is a flexible and easy to use bulk image resizer. It can resize multiple images to any specific dimension and crop images...
Read more >
How to Batch Resize Images in Windows 10 - Alphr
How to Batch Resize Multiple Images in Windows 10 Using Image Resizer for Windows · Download and follow the on-screen instructions to install ......
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