Resize image multiple times
See original GitHub issueI 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:
- Created 9 years ago
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
(ortoBuffer
orpipe
) 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 ofsharp
doesn’t exist by the second call totoFile
.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.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: