Skip to content

Commit

Permalink
Merge pull request #174 from cartuchogl/promises
Browse files Browse the repository at this point in the history
Promisify async api on promises namespace
  • Loading branch information
elad committed Oct 12, 2017
2 parents de308f0 + 3d2d0aa commit 12abd25
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
37 changes: 36 additions & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
Features

* Native bindings to the C/C++ Magick++ library
* Async, sync, and stream API
* Async, sync, stream and promises API
* Support for `convert`, `identify`, `composite`, and other utility functions

[![Build Status](https://travis-ci.org/elad/node-imagemagick-native.png)](https://travis-ci.org/elad/node-imagemagick-native)
Expand All @@ -25,6 +25,7 @@ Table of contents
* [`getConstPixels`](#getConstPixels)
* [`quantumDepth`](#quantumDepth)
* [`version`](#version)
* [Promises](#promises)
* [Installation](#installation)
* [Linux / Mac OS X](#installation-unix)
* [Windows](#installation-windows)
Expand Down Expand Up @@ -363,6 +364,40 @@ ex: 16
Return ImageMagick's version as string.
ex: '6.7.7'

<a name="promises"></a>

## Promises

The namespace promises expose functions convert, composite and identify that returns a Promise.

Examples:

````js
// convert
imagemagick.promises.convert({ /* OPTIONS */ })
.then(function(buff) { /* Write buffer */ })
.catch(function(err) { /* log err */ })

// ES8
const buff = await imagemagick.promises.convert({ /* OPTIONS */ })

// composite
imagemagick.promises.composite({ /* OPTIONS */ })
.then(function(buff) { /* Write buffer */ })
.catch(function(err) { /* log err */ })

// ES8
const buff = await imagemagick.promises.composite({ /* OPTIONS */ })

// identify
imagemagick.promises.identify({ /* OPTIONS */ })
.then(function(info) { /* Write buffer */ })
.catch(function(err) { /* log err */ })

// ES8
const info = await imagemagick.promises.identify({ /* OPTIONS */ })
````

<a name='installation'></a>

## Installation
Expand Down
19 changes: 19 additions & 0 deletions index.js
Expand Up @@ -37,3 +37,22 @@ Convert.prototype._flush = function(done) {
}

module.exports.streams = { convert : Convert };

function promisify(func) {
return function(options) {
return new Promise((resolve, reject) => {
func(options, function(err, buff) {
if (err) {
return reject(err);
}
resolve(buff);
});
});
};
}

module.exports.promises = {
convert: promisify(module.exports.convert),
identify: promisify(module.exports.identify),
composite: promisify(module.exports.composite),
};
63 changes: 63 additions & 0 deletions test/test.promises.js
@@ -0,0 +1,63 @@

var test = require('tap').test;
var imagemagick = require('..');
var debug = 0;

process.chdir(__dirname);

function saveToFileIfDebug (buffer, file) {
if (debug) {
require('fs').writeFileSync( file, buffer, 'binary' );
console.log( 'wrote file: '+file );
}
}

test( 'promise convert', function (t) {
t.plan(1);
imagemagick.promises
.convert({
srcData: require('fs').readFileSync( 'test.png' ), // 58x66
width: 100,
height: 100,
filter: 'Lagrange',
quality: 80,
format: 'PNG',
debug: debug
})
.then(function(buffer) {
t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' );
saveToFileIfDebug( buffer, 'out.png-lagrange.png' );
t.end();
});
});

test( 'promise identify', function (t) {
t.plan(5);
imagemagick.promises
.identify({
srcData: require('fs').readFileSync( 'test.png' )
})
.then(function(info){
t.equal( info.width, 58, 'width is 58' );
t.equal( info.height, 66, 'height is 66' );
t.equal( info.depth, 8, 'depth is 8' );
t.equal( info.format, 'PNG', 'format is PNG' );
t.equal( info.exif.orientation, 0, 'orientation doesnt exist' );
t.end();
});
});

test( 'promise composite', function (t) {
t.plan(1);
imagemagick.promises
.composite({
srcData: require('fs').readFileSync( 'test.quantizeColors.png' ),
compositeData: require('fs').readFileSync('test.png'),
debug: debug
})
.then(function(buffer){
t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' );
saveToFileIfDebug( buffer, 'out.composite-async.png' );
t.end();
});
});

0 comments on commit 12abd25

Please sign in to comment.