node-pngquant-native
Advanced tools
Comparing version 0.0.6 to 0.0.7
58
index.js
var _handle = require('./binding.js'); | ||
function Pngquant(buffer) { | ||
this._option = ''; | ||
this._callback = function() {}; | ||
this._handle = new _handle.Pngquant(); | ||
function Pngquant() { | ||
this.params = ''; | ||
} | ||
@@ -14,12 +12,52 @@ | ||
if (opt.params) { | ||
opt = opt.params + ' ' + opt.filename; | ||
this.params = opt.params + ' ' + opt.filename; | ||
} else { | ||
opt = opt.filename; | ||
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; | ||
} | ||
this._option = opt; | ||
return this; | ||
}, | ||
compress: function(buffer, cb) { | ||
if (cb) this._callback = cb; | ||
out = this._handle.compress(buffer, this._option, this._callback); | ||
compress: function(buffer, opt, cb) { | ||
cb = cb || function() {}; | ||
if (Object.prototype.toString.call(opt) == '[object Function]') { | ||
cb = opt; | ||
} else { | ||
this.option(opt); | ||
} | ||
var handle = new _handle.Pngquant(); | ||
out = handle.compress(buffer, this.params, cb); | ||
return out; | ||
@@ -26,0 +64,0 @@ } |
{ | ||
"name": "node-pngquant-native", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "pngquant native plugin of node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
##node-pngquant-native | ||
## node-pngquant-native | ||
[![NPM version](https://badge.fury.io/js/node-pngquant-native.png)](http://badge.fury.io/js/node-pngquant-native) | ||
@@ -6,7 +6,7 @@ | ||
##安装 | ||
## install | ||
npm install -g node-pngquant-native | ||
###环境要求 | ||
### require | ||
+ 编译pngquant,编译器必须支持C99 | ||
@@ -16,14 +16,15 @@ + 符合[node-gyp环境要求](https://github.com/TooTallNate/node-gyp#installation) | ||
##使用 | ||
## use | ||
```javascript | ||
var C = require('node-pngquant-native'); | ||
var pngquant = require('node-pngquant-native'); | ||
fs.readFile('./alphatest.png', function (err, data) { | ||
fs.readFile('./alphatest.png', function (err, buffer) { | ||
if (err) throw err; | ||
var buffer = C.option({ | ||
params: '-v --iebug' | ||
}).compress(data); | ||
fs.writeFile('./alphatest_out.png', buffer, { | ||
var resBuffer = pngquant.compress(buffer, { | ||
"speed": 1 //1 ~ 11 | ||
}); | ||
fs.writeFile('./alphatest_out.png', resBuffer, { | ||
flags: 'wb' | ||
@@ -33,3 +34,48 @@ }, function(err){}); | ||
``` | ||
## Api | ||
### pngquant.`compress(buffer, option)` | ||
```javascript | ||
var pngquant = require('node-pngquant-native') | ||
var option = { | ||
speed: 11 | ||
//... | ||
} | ||
var resBuffer = pngquant.compress(buffer, option); | ||
``` | ||
#### option | ||
+ option.`speed` | ||
Speed/quality trade-off from 1 (brute-force) to 11 (fastest). The default is 3. Speed 10 has 5% lower quality, but is 8 times faster than the default. Speed 11 disables dithering and lowers compression level. | ||
```javascript | ||
var opt = { | ||
speed: 11 | ||
} | ||
``` | ||
+ option.`quality = [min, max]` | ||
min and max are numbers in range 0 (worst) to 100 (perfect), similar to JPEG. pngquant will use the least amount of colors required to meet or exceed the max quality. If conversion results in quality below the min quality the image won't be saved (if outputting to stdin, 24-bit original will be output) and pngquant will exit with status code 99. | ||
```javascript | ||
var opt = { | ||
quality: [40, 60] | ||
} | ||
``` | ||
+ option.`iebug` | ||
Workaround for IE6, which only displays fully opaque pixels. pngquant will make almost-opaque pixels fully opaque and will avoid creating new transparent colors. | ||
```javascript | ||
var opt = { | ||
isbug: true | ||
} | ||
``` |
3773408
97
78