Search code examples
node.jsimagemagickgraphicsmagick

Multiple resizes using a single image read/stream


I'm writing a script that takes an image and creates multiple variants of different sizes (like imgur, basically). Right now, I do the following for each variant using https://github.com/aheckmann/gm in node.js:

function(file, size, newfile, callback) {
  gm(file)
    .noProfile()
    .quality(80)
    .resize(size, size)
    .write(newfile, callback)
}
  1. I assume that each time I resize the image, graphicsmagick reads the image form the disk and loads it into memory. Is this correct? If so, how can I make read from disk only once?
  2. Can I read the file from a stream, then run multiple resizes on a single stream? I don't know enough about streams, and when I tried doing this last, I got frustrated, gave up, and deleted that portion of the code.

Solution

  • Here are two commandline examples how to do it with ImageMagick.

    They use the mpr: syntax supported by ImageMagick, which tells ImageMagick to temporarily save the input image into a named memory program register, from which it can later (while processing) read the data much faster than it could do from harddisk.

    It should be easy for you to translate these commandlines into parlance.