From 6b20f9548926925cdeef57f03b73525bedf0100b Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Mon, 6 Jun 2016 16:18:21 -0700 Subject: [PATCH] 'flags' object can be set as a property of compare's options parameter. This can be used to provide any valid command-line option. --- README.md | 18 ++++++++++++++++++ lib/compare.js | 20 +++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb6be3b1..54116a00 100644 --- a/README.md +++ b/README.md @@ -568,6 +568,24 @@ gm.compare('/path/to/image1.jpg', '/path/to/another.png', options, function (err }) ``` +You can also provide a `flags` property, which you can use to directly set +additional [command-line options](http://www.imagemagick.org/script/compare.php): + +```js +var options = { + file: '/path/to/diff.png', + highlightColor: 'yellow', + tolerance: 0.02, + flags: { + '-compare': 'src', + '-colorspace': 'CMYK' + } +} +gm.compare('/path/to/image1.jpg', '/path/to/another.png', options, function (err, isEqual, equality, raw) { + ... +}) +``` + ##composite GraphicsMagick supports compositing one image on top of another. This is exposed through `gm.composite()`. Its first argument is an image path with the changes to the base image, and an optional mask image. diff --git a/lib/compare.js b/lib/compare.js index bab52622..424ae7c5 100644 --- a/lib/compare.js +++ b/lib/compare.js @@ -23,7 +23,7 @@ module.exports = exports = function (proto) { var isImageMagick = this._options && this._options.imageMagick; var appPath = this._options && this._options.appPath || ''; var bin = isImageMagick - ? appPath + 'compare' + ? appPath + 'compare' : appPath + 'gm' var args = ['-metric', 'mse', orig, compareTo] if (!isImageMagick) { @@ -37,6 +37,20 @@ module.exports = exports = function (proto) { options.highlightColor = '"' + options.highlightColor + '"'; } + // Support all command line arguments. + if (typeof options.flags === 'object') { + for (var prop in options.flags) { + if (options.flags.hasOwnProperty(prop)) { + // Expect each command-line argument to be provided in the exact + // same format as they would be used in the CL, e.g. '-compose' + // and not 'compose'. + console.log(prop, options.flags[prop]) + args.push(prop); + args.push(options.flags[prop]); + } + } + } + if (options.file) { if (typeof options.file !== 'string') { throw new TypeError('The path for the diff output is invalid'); @@ -56,13 +70,13 @@ module.exports = exports = function (proto) { } args.push(options.file); } - + if (typeof options.tolerance != 'undefined') { if (typeof options.tolerance !== 'number') { throw new TypeError('The tolerance value should be a number'); } tolerance = options.tolerance; - } + } } else { // For ImageMagick diff file is required but we don't care about it, so null it out if (isImageMagick) {