-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
126 lines (109 loc) · 3.69 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
var images = require('images'),
path = require('path'),
config = require('./config'),
fs = require('fs'),
pointerW = 0,
Imagemin = require('imagemin'),
imagemin = new Imagemin();
var imageList = []; // 从给定目录中得到的图片列表
var sortFunc = config.sortFunc || null;
function init(config) {
config.dest && fs.exists(config.dest, function (exists) {
if (!exists) {
fs.mkdirSync(config.dest);
}
});
getImageListFormDir(config.imgSrc);
}
init(config);
function getImageListFormDir(path) {
fs.exists(path, function(e) {
if (e) {
fs.readdir(path, function(err, files) {
if (err) {
console.log('[ERROR] ', '读取文件错误!');
} else {
if (files.length) {
files = files.sort(sortFunc); // 根据文件名做一个简单的排序
if (path[path.length - 1] !== '/')
path = path + '/';
files.forEach(function(file, index) {
var img = images(path + file);
imageList.push({
filename: path + file,
width: img.width(),
height: img.height()
});
});
//images.gc();
// 利用上面的参数进行合并
imageSprite(imageList);
}
}
})
} else {
console.log('[ERROR] ', path, '不存在!');
}
})
}
function imageSprite(imageList) {
var width, height,size = getImgAllWidth(imageList);
if (config.isStrict) {
width = (config.width + config.gap) * imageList.length;
height = config.height;
} else {
width = size.width;
height = size.height;
}
images.setLimit(width, height);
var bg = images(width, height);
if (!config.transparent)
bg.fill(0xff, 0xff, 0xff, 1);
var widthLabel = 0;
imageList.forEach(function(image, index) {
var img = images(image.filename);
if (config.isStrict)
img.size(config.width, config.height);
bg.draw(img, widthLabel, 0);
console.log('[INFO] 文件:', image.filename, '已拼合...');
widthLabel += img.width() + config.gap;
});
// 保存图片
config.outputName = config.outputName ? config.outputName : (Date.parse(new Date()) + '.png');
var _o = path.join(config.dest, config.outputName);
bg.save(_o, config.outputType);
console.log('[INFO] 图片保存路径!', _o);
console.log('[INFO] 开始压缩图片!');
imagemin.src(_o).dest(_o).use(Imagemin.optipng({ optimizationLevel: 3 }));
console.log('[INFO] 图片压缩完毕!');
imagemin.optimize(function (err, file) {
if (err) {
throw err;
}
// console.log(file);
// => { contents: <Buffer 89 50 4e ...>, mode: '0644' }
// 自动生成配置文件
//writeFile();
});
}
function getImgAllWidth(imageList) {
var width = 0, height = 0;
imageList.forEach(function(image) {
width += image.width;
height = image.height > height ? image.height : height;
});
return {
width: width,
height: height
};
}
function writeFile() {
fs.open('conf.css', 'w', 0644, function(e, fd) {
if (e) throw e;
fs.writeSync(fd, cssConf);
});
fs.open('conf.js', 'w', 0644, function(e, fd) {
if (e) throw e;
fs.writeSync(fd, JSON.stringify(jsConf));
});
}