1

我必须在 HTTP 响应中返回的图像上添加一些文本。我正在使用 gm (graphicmagick) 来实现它,但出现以下错误:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: spawn ENOENT
  at errnoException (child_process.js:975:11)
  at Process.ChildProcess._handle.onexit (child_process.js:766:34)

任何人都可以建议我的代码是否有任何问题:

var http = require('http')
gm = require('gm');

http.createServer(function(request, response){
    var request_options = {
        host: request.headers['host'],
        port: 80,
        path: request.path,
        method: request.method
    }

    var proxy_request = http.request(request_options, function(proxy_response){
        proxy_response.pipe(response);
        var data = new Buffer(parseInt(proxy_response.headers['content-length'],10));
        var pos = 0;
        proxy_response.on('data', function (chunk) {
          chunk.copy(data, pos);
          pos += chunk.length;
        });

        proxy_response.on('end', function () {
          gm(data, 'image.jpg')
                .drawText(10,10,"Sample")
                .stream(function streamOut (err, stdout, stderr) {
                        if (err) return "Error";
                        stdout.pipe(proxy_response); //pipe to response
                        stdout.on('error', function(){console.log("error")});
                 });
        });

        proxy_response.on('error', function (err) {
               //handle error here
        });

        response.writeHead(proxy_response.statusCode, proxy_response.headers)
    })
    request.pipe(proxy_request)
}).listen(8002)

提前致谢。

4

0 回答 0