-
Notifications
You must be signed in to change notification settings - Fork 13
/
minimage.js
215 lines (190 loc) · 4.88 KB
/
minimage.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
var path = require('path');
var fs = require('fs');
var async = require('async');
var util = require('./lib/util.js');
var logFn = console.log;
var imageOptimizeHandlerConfig = {
'jpg': optiJPEG,
'jpeg': optiJPEG,
'png': optiPNG,
'gif': optiGIF
};
var globalConfig = {
jpg: {
progressive: false
},
png: {
optimizeLevel: 7
}
}
/**
* 优化jpg图片
*/
function optiJPEG(inputFile, outputFile, callback) {
var execFile = require('child_process').execFile;
var jpegtran = require('jpegtran-bin');
var jpegtranOptions = ['-copy', 'none', '-optimize', '-outfile', outputFile, inputFile];
if (globalConfig.jpg.progressive) {
jpegtranOptions.unshift('-progressive');
}
execFile(jpegtran, jpegtranOptions, function(err) {
if (err) {
callback && callback(err);
} else {
logFn('image minified ' + outputFile);
callback && callback(null, true);
}
});
}
/**
* 优化png图片
*/
function optiPNG(inputFile, outputFile, callback) {
var execFile = require('child_process').execFile;
var optipng = require('optipng-bin');
var optipngOption = ['-clobber', '-force', '-o' + globalConfig.png.optimizeLevel, inputFile, '-out', outputFile]
execFile(optipng, optipngOption, function(err, stdout, stderr) {
if (err) {
callback && callback(err);
} else {
logFn('image minified ' + outputFile);
callback && callback(null, true);
}
});
}
/**
* 优化gif图片
*/
function optiGIF(inputFile, outputFile, callback) {
var execFile = require('child_process').execFile;
var gifsiclePath = require('gifsicle');
execFile(gifsiclePath, ['-o', outputFile, inputFile], function(err, stdout, stderr) {
if (err) {
callback && callback(err);
} else {
logFn('image minified ' + outputFile);
callback && callback(null, true);
}
});
}
/**
* 拷贝文件
*/
function copyFile(inputFile, outputFile, callback) {
if (inputFile == outputFile) {
callback && callback(null, true);
return;
}
var exec = require('child_process').exec;
exec('cp ' + inputFile + ' ' + outputFile, function(error, result) {
if (error) {
callback && console.error(error);
return;
} else {
logFn('copying file ' + outputFile);
callback && callback(null, true);
}
});
}
/**
* 文件处理(带拷贝)
*/
function _fileHandler(input, output, optCopy, callback) {
var extname = path.extname(input).toLowerCase().substring(1),
imageHandler = imageOptimizeHandlerConfig[extname];
if (!imageHandler) {
if (optCopy) {
imageHandler = copyFile;
} else {
logFn('[Error] ' + input + ' is not a image file!');
return;
}
}
util.createDirectory(output);
imageHandler(input, output, callback)
}
/**
* 文件处理
*/
function fileHandler(option) {
if (option.log && typeof(option.log) === 'function') {
logFn = option.log;
}
if (option.jpg) {
if (option.jpg.progressive) {
globalConfig.jpg.progressive = !! option.jpg.progressive;
}
}
if (option.png) {
var optimizeLevel = option.png.optimizeLevel;
if ( !! optimizeLevel && typeof(optimizeLevel) == 'number' && optimizeLevel >= 0 && optimizeLevel <= 7) {
globalConfig.png.optimizeLevel = optimizeLevel;
}
}
_fileHandler(option.input, option.output, false, option.callback)
}
/*
* 目录处理
*/
function dirHandler(option) {
if (option.log && typeof(option.log) === 'function') {
logFn = option.log;
}
if (option.jpg) {
if (option.jpg.progressive) {
globalConfig.jpg.progressive = !! option.jpg.progressive;
}
}
if (option.png) {
var optimizeLevel = option.png.optimizeLevel;
if ( !! optimizeLevel && typeof(optimizeLevel) == 'number' && optimizeLevel >= 0 && optimizeLevel <= 7) {
globalConfig.png.optimizeLevel = optimizeLevel;
}
}
function worker() {
if (fs.existsSync(option.input)) {
var files = util.traversalDirectory(option.input);
if (files && files.length > 0) {
async.eachLimit(files, 20, function(file, cb) {
var outfile = file;
if (option.input != option.output) {
outfile = file.replace(option.input, option.output);
}
_fileHandler(file, outfile, true, cb);
}, function(err) {
option.callback(err)
});
}
} else {
logFn('[Error] ' + input + ' is not exist!');
}
}
if (fs.existsSync(option.output) && option.input != option.output) {
util.deleteDirectory(option.output, worker);
} else {
worker();
}
}
/*
* 合并图片
*/
function sprite(fileMap, option, callback) {
if(typeof option == 'function') {
callback = option;
}
var sprite = require('redsprite');
var path = require('path');
var fs = require('fs');
//合并图片
sprite.create({
filemap: fileMap,
margin : option.margin || 0, //图片间隔,默认垂直排列
}, function(res) {
callback(resMap);
});
}
module.exports = {
fileHandler: fileHandler,
dirHandler: dirHandler,
sprite: sprite
};