morph.js
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Module dependencies.
*/
var fs = require('fs');
var parallel = require('array-parallel');
/**
* Extend proto.
*/
module.exports = function (proto) {
/**
* Do nothing.
*/
function noop () {}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-morph
proto.morph = function morph (other, outname, callback) {
if (!outname) {
throw new Error("an output filename is required");
}
callback = (callback || noop).bind(this)
var self = this;
if (Array.isArray(other)) {
other.forEach(function (img) {
self.out(img);
});
self.out("-morph", other.length);
} else {
self.out(other, "-morph", 1);
}
self.write(outname, function (err, stdout, stderr, cmd) {
if (err) return callback(err, stdout, stderr, cmd);
// Apparently some platforms create the following temporary files.
// Check if the output file exists, if it doesn't, then
// work with temporary files.
fs.exists(outname, function (exists) {
if (exists) return callback(null, stdout, stderr, cmd);
parallel([
fs.unlink.bind(fs, outname + '.0'),
fs.unlink.bind(fs, outname + '.2'),
fs.rename.bind(fs, outname + '.1', outname)
], function (err) {
callback(err, stdout, stderr, cmd);
})
})
});
return self;
}
}