From 8f0d27f49ecc2f63cab41c36d0f7168bcf6deda9 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Sat, 20 Apr 2013 00:19:55 -0700 Subject: [PATCH] gm().toBuffer() --- lib/command.js | 23 +++++++++++++++++++++++ test/toBuffer.js | 18 ++++++++++++++++++ test/toBufferFormat.js | 18 ++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 test/toBuffer.js create mode 100644 test/toBufferFormat.js diff --git a/lib/command.js b/lib/command.js index d8002c3..a547117 100644 --- a/lib/command.js +++ b/lib/command.js @@ -127,6 +127,29 @@ module.exports = function (proto) { return throughStream } + /** + * Convenience function for `proto.stream`. + * Simply returns the buffer instead of the stream. + * + * @param {String} format (optional) + * @param {Function} callback + * @return {null} + */ + + proto.toBuffer = function toBuffer (format, callback) { + if (!callback) callback = format, format = null; + + if ("function" !== typeof callback) { + throw new Error('gm().toBuffer() expects a callback.'); + } + + return this.stream(format, function (err, stdout) { + if (err) return callback(err); + + utils.streamToBuffer(stdout, callback); + }) + } + /** * Run any preProcessor functions in series. Used by autoOrient. * diff --git a/test/toBuffer.js b/test/toBuffer.js new file mode 100644 index 0000000..de1db96 --- /dev/null +++ b/test/toBuffer.js @@ -0,0 +1,18 @@ +var assert = require('assert'); +var fs = require('fs'); + +module.exports = function (gm, dir, finish, GM) { + + if (!GM.integration) + return finish(); + + gm + .toBuffer(function (err, buffer) { + if (err) return finish(err); + + assert.ok(buffer instanceof Buffer); + assert.ok(buffer.length); + + finish(); + }) +} diff --git a/test/toBufferFormat.js b/test/toBufferFormat.js new file mode 100644 index 0000000..e33247c --- /dev/null +++ b/test/toBufferFormat.js @@ -0,0 +1,18 @@ +var assert = require('assert'); +var fs = require('fs'); + +module.exports = function (gm, dir, finish, GM) { + + if (!GM.integration) + return finish(); + + gm + .toBuffer('PNG', function (err, buffer) { + if (err) return finish(err); + + assert.ok(buffer instanceof Buffer); + assert.ok(buffer.length); + + finish(); + }) +}