-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·98 lines (86 loc) · 2.5 KB
/
index.js
File metadata and controls
executable file
·98 lines (86 loc) · 2.5 KB
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
/**
* @file 剪切图片主文件
* @author win7killer@163.com
*/
let args = process.argv.splice(2);
let fs = require('fs');
let path = require('path');
let Jimp = require('jimp');
let colors = require('colors');
const CONF = require('./conf');
require('shelljs/global');
const comandDir = process.cwd();
let output = path.resolve(comandDir, 'output');
let numList = CONF.numList || [];
let methods = {
iNum: 0,
takeFileName() {
let aTemp = this.sFile.split('/').pop().split('.');
return {
extName: aTemp[1] || '',
fileName: aTemp[0] || ''
};
},
takeCrop() {
let fileNameMap = this.takeFileName();
Jimp.read(this.sFile)
.then((oImg) => {
this.loopCrop(oImg, fileNameMap);
})
.catch((err) => {
throw new Error(err.toString().red);
});
},
loopCrop(oImg, fMap) {
if (this.iNum < this.list.length) {
let size = this.list[this.iNum];
this.iNum++;
let outFile = `${fMap['fileName']}_${size[0]}_${size[1]}.${fMap['extName']}`;
oImg.clone()
.cover(+size[0], +size[1])
.quality(80)
.write(`${output}/${outFile}`, () => {
console.info(`${outFile} -- done`.green);
this.loopCrop(oImg, fMap);
});
} else {
console.info('------------ success!! -- all done ------------'.rainbow);
return;
}
},
arrUniq() {
return Array.from(new Set(numList));
},
init() {
if (!args[0] || /^\d+$/.test(args[0])) {
echo('请指定要处理的图片,如“cimg 1.jpg”\n'.bgRed);
exit(1);
}
// 文件处理
if (/^https?\:/.test(args[0])) { // url
this.sFile = args[0];
} else {
this.sFile = path.resolve(comandDir, args[0]);
if (!fs.existsSync(this.sFile)) {
echo(`文件 ${this.sFile} 不存在\n`.bgRed);
exit(1);
} else {
echo(`处理图片···\n${this.sFile}`.bgMagenta);
}
}
// 剪裁list处理
if (!args[1] && !args[2]) {
this.list = this.arrUniq();
} else {
this.list = [
[args[1], args[2]]
];
}
this.takeCrop();
}
};
module.exports = {
init: function () {
methods.init();
}
}