Skip to content Skip to sidebar Skip to footer

Node.js Asynchronous If Statements GraphicsMagick

I am trying to use the GraphicsMagick library in my Node.js app, to manipulate images on the fly. After the image has been selected using: var image = gm('/path/to/image.jpg'); I

Solution 1:

GraphicsMagick will not make any executions and work when you are calling methods on image like: resize, blur etc. an they are very lightweight.

In fact all they do - is adding arguments to the chain (strings to array of arguments). So the object that gm() returns - is chain object, and does not do much until you will performwrite method.

When write method is called it will in fact spawn process that all arguments will be passed to, and this is place where all calculation happens, and that is why it is async.

So in order to make your optional methods - you do exactly the way you need to do it:

if(blur){
  image = image.blur(blur1, blur2);
}
if(scale){
  image = image.resize(resizeX, resizeY);
}
if(sepia){
  image = image.sepia();
}

At the end image will contain array of arguments, and calling write to it, will execute all of them. Every time you call any of those methods which in fact you can have a look here: https://github.com/aheckmann/gm/blob/master/lib/args.js All they do is return new modified object with added arguments.

Here is more details and explanation over this question and how it works: https://stackoverflow.com/a/17745079/1312722


Post a Comment for "Node.js Asynchronous If Statements GraphicsMagick"