这段代码用于根据设置的高度和宽度参数创建动态图像。假设 localhost:3000/50/50 将给出宽度为 50、高度为 50 的图像。我正在使用从 github 获得的这段代码。我已经在我的系统中安装了 imageMagick。
var http = require('http');
var url = require('url');
var fs = require('fs');
var gm = require('gm');
var server = http.createServer(function(request, response){
var url_parts = url.parse(request.url).path.substring(1).split("/");
var width = parseInt(url_parts[0]);
var height = parseInt(url_parts[1]);
var max = Math.max(width, height);
if(!isNaN(width) && !isNaN(height))
{
response.writeHead(200, {'content-type': 'image/png'});
gm('nodejs.png').
resize(max, max).
crop(width, height, 0, 0).
stream(function(err, stdout, stderr){
if(err) {
console.log(err)
}
else {
stdout.pipe(response);
}
});
}
else {
response.writeHead(400, {'content-type' : 'text/plain'});
response.end();
}
})
.listen(3000);
这是我得到的错误
events.js:72 throw er; // Unhandled 'error' event ^ Error: spawn ENOENT at errnoException (child_process.js:980:11) at Process.ChildProcess._handle.onexit (child_process.js:771:34)
文件nodejs.png与项目位于同一目录中。我做错了什么?
最佳答案
几乎可以肯定,您需要安装 ImageMagic 或 GraphicsMagic。我的猜测是 gm 模块只是图形管理命令行工具的包装。因此,当您调用像 resize()
这样的东西时,node
将尝试调用未找到的 /usr/bin/convert
,因此您会收到 spawn child_process
错误。
要安装 imagemagic,您可以在 Ubuntu 中输入 sudo apt-get install imagemagic
。
关于image - Node js 动态图像 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21331798/