-
Notifications
You must be signed in to change notification settings - Fork 3
/
magickal.js
73 lines (59 loc) · 1.92 KB
/
magickal.js
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
62
63
64
65
66
67
68
69
70
71
72
73
(function() {
var sys = require('sys');
var Magickal = {}
Magickal.Image = function(input) {
this.input = input;
};
Magickal.Image.prototype = {
inArgs : [],
outArgs : [],
cropResized : function(width, height, gravity) {
return this.resize(width, height).crop(width, height)
},
resize : function(width, height) {
var wh = width + "x" + height;
return this.makeArgs(["-resize", wh]);
},
crop : function(width, height) {
var wh = width + "x" + height;
return this.makeArgs(["-crop", wh]);
},
makeArgs : function(inargs, outargs) {
if (arguments.length == 1) {
outargs = inargs;
inargs = null;
}
if (inargs) {
this.inArgs = this.inArgs.concat(inargs);
}
if (outargs) {
this.outArgs = this.outArgs.concat(outargs);
}
return this;
},
write : function(out, callback) {
this.inArgs.push(this.input);
this.outArgs.push(out);
var args = this.inArgs.concat(this.outArgs);
this.__run("convert", args, callback);
},
__run : function (cmd, args, callback) {
args.unshift(cmd);
cmd = "gm";
sys.puts("running command: " + cmd + " " + args.join(" "));
var p = process.createChildProcess(cmd, args);
p.addListener("output", callback);
p.addListener("error", this.errorHandler);
p.addListener("exit", this.onExit(p));
},
onExit : function(proc) {
return function (data) {}
},
errorHandler : function(err) {
if (err) sys.puts(err);
}
}
exports.image = function(input) {
return new Magickal.Image(input);
};
})();