forked from hacksparrow/node-easyimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyimage.js
279 lines (200 loc) · 8.5 KB
/
easyimage.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var Q = require('q');
var exec = require('child_process').execFile;
var child, args;
var error_messages = {
'path': 'Missing image paths.\nMake sure both source and destination files are specified.',
'dim': 'Missing dimensions.\nSpecify the width atleast.',
'restricted': 'The command you are trying to execute is prohibited.',
'unsupported': 'File not supported.',
};
// general info function
function info(file) {
var deferred = Q.defer();
//file = quoted_name(file);
// %z = depth, %m = type, %w = width, %h = height, %b = filesize in byte, %f = filename, %x = density
args = ['-format', '%m %z %w %h %b %x %f', file];
child = exec('identify', args, function(err, stdout, stderr) {
var info = {};
//Basic error handling
if (stdout) {
var temp = stdout.replace('PixelsPerInch', '').replace('PixelsPerCentimeter', '').split(' ');
//Basic error handling:
if (temp.length < 7) {
deferred.reject(new Error(stderr || error_messages['unsupported']));
} else {
info.type = temp[0];
info.depth = parseInt(temp[1]);
info.width = parseInt(temp[2]);
info.height = parseInt(temp[3]);
info.size = parseInt(temp[4]);
info.density = parseFloat(temp[5]);
info.name = temp.slice(6).join(' ').replace(/(\r\n|\n|\r)/gm, '').trim();
if (stderr) {
info.warnings = stderr.split('\n');
}
deferred.resolve(info);
}
} else {
deferred.reject(new Error(stderr || error_messages['unsupported']));
}
});
return deferred.promise;
}
// function to quote file names, if not already
function quoted_name(name) {
if (name[0] != '"') name = '"' + name;
if (name[name.length - 1] != '"') name = name + '"';
return name;
};
// get basic information about an image file
exports.info = function(file) {
return info(file);
};
// convert a file type to another
exports.convert = function(options) {
var deferred = Q.defer();
process.nextTick(function () {
if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']);
// options.src = quoted_name(options.src);
// options.dst = quoted_name(options.dst);
if (options.quality === undefined) args = [options.src, options.dst];
else args = [options.src, '-quality', options.quality, options.dst];
child = exec('convert', args, function(err, stdout, stderr) {
if (err) deferred.reject(err);
else deferred.resolve(info(options.dst));
});
})
return deferred.promise;
};
// rotate a file
exports.rotate = function(options) {
var deferred = Q.defer();
process.nextTick(function () {
if (options.src === undefined || options.dst === undefined || options.degrees === undefined) return deferred.reject(error_messages['path']);
args = [options.src, '-rotate', options.degrees, options.dst];
child = exec('convert', args, function(err, stdout, stderr) {
if (err) deferred.reject(err);
else deferred.resolve(info(options.dst));
});
})
return deferred.promise;
};
// resize an image
exports.resize = function(options) {
var deferred = Q.defer();
process.nextTick(function () {
if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']);
if (options.width === undefined) return deferred.reject(error_messages['dim']);
options.height = options.height || options.width;
// options.src = quoted_name(options.src);
// options.dst = quoted_name(options.dst);
if (options.quality === undefined) args = [options.src, '-resize', options.width + 'x' + options.height, options.dst];
else args = [options.src, '-resize', options.width + 'x' + options.height, '-quality', options.quality, options.dst];
child = exec('convert', args, function(err, stdout, stderr) {
if (err) deferred.reject(err);
deferred.resolve(info(options.dst));
});
})
return deferred.promise;
};
// crop an image
exports.crop = function(options) {
var deferred = Q.defer();
if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']);
if (options.cropwidth === undefined) return deferred.reject(error_messages['dim']);
options.cropheight = options.cropheight || options.cropwidth;
options.x = options.x || 0;
options.y = options.y || 0;
// options.src = quoted_name(options.src);
// options.dst = quoted_name(options.dst);
args = [options.src, '-crop', options.cropwidth + 'x' + options.cropheight + '+' + options.x + '+' + options.y, options.dst];
if (options.quality !== undefined) {
args.splice(1, 0, '-quality');
args.splice(2, 0, options.quality);
}
if (options.gravity !== undefined) {
args.splice(1, 0, '-gravity');
args.splice(2, 0, options.gravity)
}
child = exec('convert', args, function(err, stdout, stderr) {
if (err) deferred.reject(err);
deferred.resolve(info(options.dst));
});
return deferred.promise;
};
// resize and crop in one shot!
exports.rescrop = function(options) {
var deferred = Q.defer();
process.nextTick(function () {
if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']);
if (options.width === undefined) return deferred.reject(error_messages['dim']);
options.height = options.height || options.width;
options.cropwidth = options.cropwidth || options.width;
options.cropheight = options.cropheight || options.height;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
options.y = options.y || 0;
// options.src = quoted_name(options.src);
// options.dst = quoted_name(options.dst);
options.fill = options.fill ? '^' : '';
if (options.quality === undefined) args = [options.src, '-resize', options.width + 'x' + options.height + options.fill, '-gravity', options.gravity, '-crop', options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y, options.dst];
else args = [options.src, '-resize', options.width + 'x' + options.height, options.fill, '-gravity' + options.gravity, '-crop', options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y, '-quality', options.quality, options.dst];
child = exec('convert', args, function(err, stdout, stderr) {
if (err) deferred.reject(err);
deferred.resolve(info(options.dst));
});
})
return deferred.promise;
};
// create thumbnails
exports.thumbnail = function(options) {
var deferred = Q.defer();
process.nextTick(function () {
if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']);
if (options.width === undefined) return deferred.reject(error_messages['dim']);
options.height = options.height || options.width;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
options.y = options.y || 0;
// options.src = quoted_name(options.src);
// options.dst = quoted_name(options.dst);
info(options.src).then(function(original) {
// dimensions come as strings, convert them to number
original.width = +original.width;
original.height = +original.height;
var resizewidth = options.width;
var resizeheight = options.height;
if (original.width > original.height) { resizewidth = ''; }
else if (original.height > original.width) { resizeheight = ''; }
// resize and crop
if (options.quality === undefined) args = [options.src, '-interpolate', 'bicubic', '-strip', '-thumbnail', resizewidth + 'x' + resizeheight, '-gravity', options.gravity, '-crop', options.width + 'x'+ options.height + '+' + options.x + '+' + options.y, options.dst];
else args = [options.src, '-interpolate', 'bicubic', '-strip', '-thumbnail', resizewidth + 'x' + resizeheight, '-quality', options.quality, '-gravity', options.gravity, '-crop', options.width + 'x'+ options.height + '+' + options.x + '+' + options.y, options.dst];
child = exec('convert', args, function(err, stdout, stderr) {
if (err) return deferred.reject(err);
deferred.resolve(info(options.dst));
});
}, function (err) { deferred.reject(err); });
})
return deferred.promise;
};
// issue your own ImageMagick command
exports.exec = function(command) {
var deferred = Q.defer();
process.nextTick(function () {
// Check if command is String or Array
if (Object.prototype.toString.call(command).toLowerCase() === '[object array]') {
args = command.slice(0); // Make a copy
} else {
args = command.split(' ');
}
// Slice args only if first element is command
if (args.length && args[0] === 'convert') {
args = args.slice(1);
}
child = exec('convert', args, function(err, stdout, stderr) {
if (err) return deferred.reject(err);
deferred.resolve(stdout);
});
})
return deferred.promise;
};