-
Notifications
You must be signed in to change notification settings - Fork 7
/
toolbelt.js
179 lines (149 loc) · 4.33 KB
/
toolbelt.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
var fs = require('q-io/fs'),
path = require('path'),
gm = require('gm'),
q = require('q'),
proc = require('child_process');
console.time('work took me');
var dirify = function dirify(path) {
return __dirname + '/' + path;
};
var filterByExtensions = function filterByExtensions(extensions, filesList) {
return filesList.filter(function(file) {
var extension = file.split('.'),
extension = extension[extension.length - 1];
return extensions[extension];
});
};
var jsOnly = filterByExtensions.bind(null, {
js: 1
}),
imagesOnly = filterByExtensions.bind(null, {
png: 1,
jpg: 1
}),
pngOnly = filterByExtensions.bind(null, {
png: 1
}),
jpgOnly = filterByExtensions.bind(null, {
jpg: 1
});
var imageSize = function imageSize(image) {
var deffered = q.defer();
gm(image).size(function(error, value) {
if (error) {
deffered.reject(new Error(error));
} else {
deffered.resolve({
path: image,
size: value
});
}
});
return deffered.promise;
};
var extentImage = function extentImage(image, size) {
var deffered = q.defer();
gm(image).extent(size.width, size.height)
.write(image, function(error) {
if (error) {
deffered.reject(new Error(error));
} else {
deffered.resolve(image);
}
});
return deffered.promise;
};
var equateImagesHeight = function equateImagesHeight(images) {
var deffered = q.defer(),
sizes = [];
images.forEach(function(image) {
image = __dirname + '/' + image;
sizes.push(imageSize(image));
});
q.all(sizes)
.then(function(images) {
var lower, higher;
if (images[0].size.height > images[1].size.height) {
lower = 1;
higher = 0;
} else if (images[0].size.height < images[1].size.height) {
lower = 0;
higher = 1;
} else {
deffered.resolve(images);
return deffered.promise;
}
extentImage(images[lower].path, images[higher].size)
.then(function(image) {
deffered.resolve(image);
})
.fail(function(error) {
deffered.reject(error);
});
});
return deffered.promise;
};
var compareImages = function compareImages(images, file) {
var deffered = q.defer();
gm.compare(images[0], images[1], {highlightColor: 'red2', file: file}, function (err, isEqual, equality) {
if (err) {
deffered.reject(new Error(err));
} else {
deffered.resolve({
isEqual: isEqual,
equality: equality
})
}
});
return deffered.promise;
};
var changeExtension = function changeExtension(ext, path) {
return path.split('.')[0] + '.' + ext;
};
var toJpg = changeExtension.bind(null, 'jpg');
var stripToJpg = function stripToJpg(image, quality) {
var deffered = q.defer(),
jpg = toJpg(image);
gm(image).noProfile().comment('').interlace('Plane').quality(quality)
.write(jpg, function(error) {
if (error) {
deffered.reject(new Error(error));
} else {
deffered.resolve(image);
}
});
return deffered.promise;
};
var exec = function exec(command) {
var deffered = q.defer();
proc.exec(command, function(error, stdout, stderr) {
if (error) {
deffered.reject({
error: new Error(error),
stdout: stdout
});
} else {
deffered.resolve({
stdout: stdout,
stderr: stderr
});
}
});
return deffered.promise;
};
module.exports = {
dirify: dirify,
filterByExtensions: filterByExtensions,
jsOnly: jsOnly,
imagesOnly: imagesOnly,
pngOnly: pngOnly,
jpgOnly: jpgOnly,
imageSize: imageSize,
extentImage: extentImage,
equateImagesHeight: equateImagesHeight,
compareImages: compareImages,
changeExtension: changeExtension,
toJpg: toJpg,
stripToJpg: stripToJpg,
exec: exec
};