节点 gm 流上出错

Error on Node gm Stream

本文关键字:出错 gm 节点      更新时间:2023-09-26

我有以下Buffer对象数组,这些对象是从由jpgpng图像组成的编码图像字符串base64创建的:

[ 
    <Buffer 75 ab 5a 8a 66 a0 7b f8 e9 7a 06 da b1 ee b8 ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 03 02 02 03 03 03 03 ... >,
    <Buffer 75 ab 5a 8a 66 a0 7b f8 e9 7a 06 da b1 ee b8 ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 03 02 02 03 03 03 03 ... >
]

我正在尝试使用Node.jsgm将图像拼接在一起。

var currentGm = gm(images.shift());
for (var i=0; i<images.length; i++) {
    currentGm.append(images[i]);
}

然后将该数据piperes jpg .

currentGm.stream('jpg').pipe(res);

但是我收到此错误:

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: spawn gm ENOENT
    at exports._errnoException (util.js:856:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at nextTickCallbackWith2Args (node.js:478:9)
    at process._tickDomainCallback (node.js:433:17)

我尝试检查err stream回调,它null.

currentGm也看起来像这样:

gm {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _options: {},
  data: {},
  _in: [],
  _out: [],
  _outputFormat: null,
  _subCommand: 'convert',
  sourceBuffer: <Buffer 75 ab 5a 8a 66 a0 7b f8 e9 7a 06 da b1 ee b8 ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 03 02 02 03 03 03 03 ... >,
  source: 'unknown.jpg',
  _sourceFormatters: [ [Function] ] }

回调stdout对象如下所示:

Socket {
  _connecting: false,
  _hadError: false,
  _handle:
   Pipe {
     _externalStream: {},
     fd: 27,
     writeQueueSize: 0,
     owner: [Circular],
     onread: [Function: onread],
     reading: true },
  _parent: null,
  _host: null,
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: true,
     sync: false,
     needReadable: true,
     emittedReadable: false,
     readableListening: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events:
   { end: { [Function: g] listener: [Function: onend] },
     finish: [Function: onSocketFinish],
     _socketEnd: [Function: onSocketEnd],
     close: [Function] },
  _eventsCount: 4,
  _maxListeners: undefined,
  _writableState:
   WritableState {
     objectMode: false,
     highWaterMark: 16384,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     decodeStrings: false,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false },
  writable: false,
  allowHalfOpen: false,
  destroyed: false,
  bytesRead: 0,
  _bytesDispatched: 0,
  _sockname: null,
  _writev: null,
  _pendingData: null,
  _pendingEncoding: '' }

在 Mac OS X 上,使用 brew 安装 imagemagick 修复了此问题。

brew install imagemagick

在我的router

var gm = require('gm').subClass({ imageMagick: true });