image - Node js 动态图像 - 错误

标签 image node.js

这段代码用于根据设置的高度和宽度参数创建动态图像。假设 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/

相关文章:

javascript - Jest 测试涉及当前日期的函数

node.js - Node TLS 客户端中出现神秘 TCP 错误

node.js - 从 localhost 网站执行 linux 命令

image - 使用 Processing 保存图像

C++如何使用setpixel函数绘制色调曲线( Gamma )以绘制图形

html - 如何在所有 Web 浏览器中显示 TIFF 图像?

java - 检查Jlabel是否有Icon

java - 如何将图像合并到 SQLite 测验中?

javascript - Mongodb node driver 2.0.* with Bluebird 2.9.* promisification

javascript - 如何在批量导入中使用sequelize findorcreate?