-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
73 lines (64 loc) · 2.42 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
var semver = require('semver');
var _handle = require('./scripts/binding.js')();
function Pngquant() {
this.params = '';
}
Pngquant.prototype = {
option: function (opt) {
opt = opt || {};
opt.filename = opt.filename || 'stdin.png';
if (opt.params) {
this.params = opt.params + ' ' + opt.filename;
} else {
var toString = Object.prototype.toString;
var params = [];
for (var p in opt) {
if (opt.hasOwnProperty(p)) {
switch(p) {
case 'quality':
var quality = opt[p];
if (toString.call(quality) == '[object Array]') {
var l = parseInt(quality[0]), h = parseInt(quality[1]);
if (l < 0) {
l = 0;
}
if (h > 100) {
h = 100;
}
params.push('--quality=' + l + '-' + h)
}
break;
case 'speed':
var speed = parseInt(opt[p]);
if (!speed) speed = 1;
params.push('--speed='+speed);
break;
case 'iebug':
if (opt[p]) {
params.push('--iebug');
}
break;
}
}
}
this.params = params.join(' ') + ' ' + opt.filename;
}
return this;
},
compress: function(buffer, opt) {
var out;
if (Object.prototype.toString.call(opt) == '[object Object]') {
this.option(opt);
}
if (semver.gte(process.versions.node, '10.0.0')) {
out = (new _handle.Pngquant(buffer, this.params)).compress();
} else if (semver.gte(process.versions.node, '4.0.0')) {
out = (new _handle(buffer, this.params)).compress();
} else {
var handle = new _handle.Pngquant();
out = handle.compress(buffer, this.params, function nope(){});
}
return out;
}
};
module.exports = new Pngquant;