Sunday, 30 April 2017

Resize one image multiple times

I am resizing a given image (saved on disk) to differenz 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?


via Chris

No comments:

Post a Comment