UNPKG

3.02 kBJavaScriptView Raw
1
2/**
3 * Module dependencies.
4 */
5
6var Stream = require('stream').Stream;
7var EventEmitter = require('events').EventEmitter;
8var util = require('util');
9
10util.inherits(gm, EventEmitter);
11
12/**
13 * Constructor.
14 *
15 * @param {String|Number} path - path to img source or ReadableStream or width of img to create
16 * @param {Number} [height] - optional filename of ReadableStream or height of img to create
17 * @param {String} [color] - optional hex background color of created img
18 */
19
20function gm (source, height, color) {
21 var width;
22
23 if (!(this instanceof gm)) {
24 return new gm(source, height, color);
25 }
26
27 EventEmitter.call(this);
28
29 this._options = {};
30 this.options(this.__proto__._options);
31
32 this.data = {};
33 this._in = [];
34 this._out = [];
35 this._outputFormat = null;
36 this._subCommand = 'convert';
37
38 if (source instanceof Stream) {
39 this.sourceStream = source;
40 source = height || 'unknown.jpg';
41 } else if (Buffer.isBuffer(source)) {
42 this.sourceBuffer = source;
43 source = height || 'unknown.jpg';
44 } else if (height) {
45 // new images
46 width = source;
47 source = "";
48
49 this.in("-size", width + "x" + height);
50
51 if (color) {
52 this.in("xc:"+ color);
53 }
54 }
55
56 if (typeof source === "string") {
57 // then source is a path
58
59 // parse out gif frame brackets from filename
60 // since stream doesn't use source path
61 // eg. "filename.gif[0]"
62 var frames = source.match(/(\[.+\])$/);
63 if (frames) {
64 this.sourceFrames = source.substr(frames.index, frames[0].length);
65 source = source.substr(0, frames.index);
66 }
67 }
68
69 this.source = source;
70
71 this.addSrcFormatter(function (src) {
72 // must be first source formatter
73
74 var inputFromStdin = this.sourceStream || this.sourceBuffer;
75 var ret = inputFromStdin ? '-' : this.source;
76
77 if (ret && this.sourceFrames) ret += this.sourceFrames;
78
79 src.length = 0;
80 src[0] = ret;
81 });
82}
83
84/**
85 * Subclasses the gm constructor with custom options.
86 *
87 * @param {options} options
88 * @return {gm} the subclasses gm constructor
89 */
90
91var parent = gm;
92gm.subClass = function subClass (options) {
93 function gm (source, height, color) {
94 if (!(this instanceof parent)) {
95 return new gm(source, height, color);
96 }
97
98 parent.call(this, source, height, color);
99 }
100
101 gm.prototype.__proto__ = parent.prototype;
102 gm.prototype._options = {};
103 gm.prototype.options(options);
104
105 return gm;
106}
107
108/**
109 * Augment the prototype.
110 */
111
112require("./lib/options")(gm.prototype);
113require("./lib/getters")(gm);
114require("./lib/args")(gm.prototype);
115require("./lib/drawing")(gm.prototype);
116require("./lib/convenience")(gm.prototype);
117require("./lib/command")(gm.prototype);
118require("./lib/compare")(gm.prototype);
119require("./lib/composite")(gm.prototype);
120require("./lib/montage")(gm.prototype);
121
122/**
123 * Expose.
124 */
125
126module.exports = exports = gm;
127module.exports.utils = require('./lib/utils');
128module.exports.compare = require('./lib/compare')();
129module.exports.version = require('./package.json').version;