-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (122 loc) · 3.47 KB
/
index.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
#! /usr/bin/env node
let Jimp = require('jimp'),
path = require('path'),
_ = require('lodash'),
color = require('colors'),
q = require('q'),
execFile = require('child_process').execFile,
Args = require('./args'),
File = require('./file');
require('./utils/col');
require('./utils/ends-with-any');
class ImageReducer {
constructor() {
if (!Args.validate()) {
process.exit();
}
this.run();
}
run() {
let promises = null;
let args = Args.get();
// single image
if (args.img) {
promises = [
this.reduceImage(args.img)
];
// dir
} else if (args.dir) {
let images = File
.readDir(args.dir)
.filter((file) => file.endsWithAny(['.png', '.jpg', '.jpeg']));
images.length && console.log(`\nReducing ${images.length} images...`);
promises = _.map(images, (image) => this.reduceImage(image));
}
let timeStart = Date.now();
if (!promises.length) {
console.log(`No images found in ${args.dir}`.yellow);
}
q
.all(promises)
.spread((...processedImages) => this.printResults(processedImages))
.then(() => {
console.log('\nTime:', Date.now() - timeStart, 'ms');
});
}
reduceImage(imagePath) {
let defer = q.defer();
let args = Args.get();
let imageName = File.getFileName(imagePath);
Jimp.read(imagePath, (err, img) => {
if (err) {
defer.reject(err);
};
let outputPath = path.join(args.out, imageName);
img
.scale(args.scale)
.quality(args.quality)
.blur(args.blur)
.brightness(args.brightness)
.write(outputPath, () => {
let originalSize = File.size(imagePath) / 1000; // KB
let outputSize = File.size(outputPath) / 1000;
defer.resolve({
imageName,
originalSize,
outputSize,
});
});
});
return defer.promise;
}
reduceImageParallel(imagePath) {
let defer = q.defer();
let args = Args.get();
execFile('node', ['./reduce-image-task', `--image=${imagePath}`, `--out=${args.out}`], (error, stdout, stderr) => {
if (error) {
return defer.reject(error);
}
defer.resolve(JSON.parse(stdout));
});
return defer.promise;
}
printResults(processedImages) {
function row(...cols) {
console.log('| ' + _.join(_.flatten(cols), ' | ') + ' |');
}
if (!processedImages.length) {
return;
}
function header(...cols) {
return row(_.map(cols, (col) => {
return col
.toUpperCase()
.bold
.bgWhite;
}));
}
console.log(`\n${processedImages.length} image(s) processed\n`.green);
const nameColSize = _.reduce(processedImages, (max, img) => Math.max(max, img.imageName.length), 0);
const fromColSize = 8;
const toColSize = 6;
const lossColSize = 8;
// header
header(
'File'.col(nameColSize),
'Frm KB'.col(fromColSize),
'To KB'.col(toColSize),
'Loss'.col(lossColSize)
);
_.each(processedImages, (image) => {
let {imageName, originalSize, outputSize} = image;
let sizeReduction = (100 - (outputSize / originalSize * 100)).toFixed(2);
row(
imageName.col(nameColSize).bold,
originalSize.toFixed(2).col(fromColSize).blue,
outputSize.toFixed(2).col(toColSize).green,
(sizeReduction + ' %').col(lossColSize).bold
);
});
}
}
new ImageReducer();