forked from hacksparrow/node-easyimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyimage.js
174 lines (147 loc) · 7.33 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
var exec = require('child_process').exec;
var child;
var imcmd; // short for ImageMagick Command, how ingenious (y)
// treasure of error messages
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, callback) {
if (callback === undefined)return;
file = quoted_name(file);
// %z = depth, %m = type, %w = width, %h = height, %b = filesize in byte, %f = filename, %x = density
imcmd = 'identify -format "%m %z %w %h %b %x %f" ' + file;
child = exec(imcmd, function(err, stdout, stderr) {
var info = {};
//Basic error handling
if (stderr.match(/^identify:/)) {
return callback(error_messages['unsupported'], stdout, stderr);
} else {
var temp = stdout.split(' ');
//Basic error handling:
if (temp.length < 7) {
return callback(error_messages['unsupported'], stdout, stderr);
} 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,'');
return callback(err, info, stderr);
}
}
});
}
// 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, callback) {
info(file, callback);
};
// convert a file type to another
exports.convert = function(options, callback) {
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
options.src = quoted_name(options.src);
options.dst = quoted_name(options.dst);
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) return callback(err);
info(options.dst, callback);
});
};
// resize an image
exports.resize = function(options, callback) {
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(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) imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) return callback(err);
info(options.dst, callback);
});
};
// crop an image
exports.crop = function(options, callback) {
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.cropwidth === undefined)return callback(error_messages['dim']);
options.cropheight = options.cropheight || options.cropwidth;
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);
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) return callback(err);
info(options.dst, callback);
});
};
// resize and crop in one shot!
exports.rescrop = function(options, callback) {
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(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) imcmd = 'convert ' + 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 imcmd = 'convert ' + 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(imcmd, function(err, stdout, stderr) {
if (err) return callback(err);
info(options.dst, callback);
});
};
// create thumbnails
exports.thumbnail = function(options, callback) {
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(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, function(err, original, stderr) {
if (err) return callback(err);
// 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) imcmd = 'convert ' + options.src + ' -resize ' + resizewidth + 'x' + resizeheight + ' -gravity ' + options.gravity + ' -crop '+ options.width + 'x'+ options.height + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize '+ resizewidth + 'x' + resizeheight + ' -quality ' + options.quality + ' -gravity ' + options.gravity + ' -crop '+ options.width + 'x'+ options.height + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) return callback(err);
info(options.dst, callback);
});
});
};
// for the hardcore types - issue your own ImageMagick command
exports.exec = function(command, callback) {
var _command = command.split(' ')[0];
// as a security measure, we will allow only 'convert' commands
if (_command != 'convert')return callback(error_messages['restricted']);
child = exec(command, function(err, stdout, stderr) { callback(err, stdout, stderr); });
};