简体   繁体   English

Node.js异步if语句GraphicsMagick

[英]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. 我正在尝试使用Node.js应用程序中的GraphicsMagick库来动态处理图像。

After the image has been selected using: 选择图像后,使用:

var image = gm('/path/to/image.jpg');

I want to perform several actions on it. 我要对其执行一些操作。 The issue I am facing is because the actions to be performed come from variables. 我面临的问题是因为要执行的操作来自变量。 For example, if blur is true, it should blur the image. 例如,如果模糊为真,则应该使图像模糊。 If scale is also true, the blurred image should then be scaled. 如果比例尺也为真,则应缩放模糊图像。 The trouble is, GraphicsMagic library is asynchronous, so this script would result in many actions being performed at the same time, which might turn out horribly. 问题在于,GraphicsMagic库是异步的,因此此脚本将导致同时执行许多操作,这可能会令人发指。

The functions do accept callbacks, as shown on this example on GitHub. 这些函数确实接受回调,如GitHub上的示例所示。 Although they appear synchronous, this answer here confirms that they're asynchronous. 尽管它们看起来是同步的,但这里的答案证实了它们是异步的。 New answer here shows that the functions are synchronous. 这里的答案表明这些功能是同步的。

How can I perform the actions on the image one by one, while staying non-blocking, when I don't know which actions are being performed? 当我不知道正在执行哪些操作时,如何在不阻塞的情况下在图像上一一执行操作?

I was thinking something along the lines of a NextAction() function which would be executed in the callback. 我正在考虑在回调中执行的NextAction()函数。 The NextAction() would then trigger the next action, but I'm not sure about how to go about this. 然后,NextAction()将触发下一个动作,但是我不确定如何执行此操作。

I have researched StratifiedJS, but decided against it as I don't want to further complicate my app, and I don't think my PaaS supports it. 我已经研究了StratifiedJS,但是由于我不想进一步使我的应用程序复杂化,并且我不认为我的PaaS支持它,因此决定反对它。

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

GraphicsMagick will not make any executions and work when you are calling methods on image like: resize, blur etc. an they are very lightweight. 当您在图像上调用诸如调整大小,模糊等图像上的方法时, GraphicsMagick将不会执行任何工作,并且它们非常轻巧。

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 perform write method. 因此gm()返回的对象-是链对象,在执行write方法之前不会做很多事情。

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. 实际上,当调用write方法时,它将产生所有参数都将传递到的过程,而这正是所有计算发生的地方,这就是为什么它是异步的。

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. 在结束image包含的参数数组,并要求write它,将执行所有的人。 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. 每次您调用其中任何一种实际上可以在这里查看的方法时,都可以在这里查看: https : //github.com/aheckmann/gm/blob/master/lib/args.js他们所做的只是返回带有添加参数的新修改对象。

Here is more details and explanation over this question and how it works: https://stackoverflow.com/a/17745079/1312722 这是有关此问题及其工作原理的更多详细信息和解释: https : //stackoverflow.com/a/17745079/1312722

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM