Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize one image multiple times

Tags:

node.js

sharp

I am resizing a given image (saved on disk) to different sizes:

var image = 'photos/pic';
var sizes = [1440, 1080, 720, 480];

for (var i = 0; i < sizes.length; i++) {
    sharp(image + '.jpg')
    .resize(sizes[i], sizes[i])
    .toFile(image + '-' + sizes[i] + '.jpg');       
}

This works as expected, but I guess there is room for improvements.

  • Will the for-loop lead to any problems? If yes, is there a better way to solve that?
  • Would it be faster to wait for the generated picture to be resized and use this for the next resizing process? Let's say the original picture is 2000x2000. What's the speed improvement from resizing 720x720 to 480x480 instead of 2000x2000 to 480x480, if there is any? Considering I have to read the 720x720 file first and wait for the resizing to be finished.
  • Should I do those resizes on the "main" node thread or fork a child process? They are running asynchronously anyways, correct?
like image 698
Chris Avatar asked Apr 30 '17 08:04

Chris


People also ask

Is there a way to batch resize photos?

If you're working on a photoshoot and need all your photos resized into a specific format, look no further than Adobe Lightroom's bulk resize feature. This feature allows you to crop all your photos to a fixed size and output them at a certain set of pixel dimensions to create the highest image quality.

How do I batch resize multiple images?

Resize multiple images by batch processing them Now you can batch process your images to resize them all. To do this, open Photoshop, then go to File > Automate > Batch. You should now see the Batch window. Choose the set that you created your action in, and then choose your action.


1 Answers

you can do it by using bluebird promise

const Promise = require('bluebird');
global.Promise = Promise;


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 );
});

src :- https://github.com/lovell/sharp/issues/154

i tried it and it is working 100%

like image 77
Fadi Abo Msalam Avatar answered Oct 08 '22 01:10

Fadi Abo Msalam