Optipng.js
Optipng.js is the port of optipng in javascript using emscripten. You can optimize png image file without losing any information in the morden browser using Optipng.js.
Tip: Optipng version is 0.7.7.
API
optipng(file, options, printFunction)
file
Please use binary file like readFile on node or Uint8Array (converted from base64) on javascript.
// Node.jsvar input = fs;var output = ;
// Browser { var arr = dataurl mime = arr01 bstr = n = bstrlength u8arr = n; while n-- u8arrn = bstr; return u8arr;} { var fileReader = ; fileReader { var ary = ; ; }; fileReader;} var input output;;
options
Options can be array or object.
var options = "-o2" "-i0" "-strip" "all";var options = o2: true i0: true strip: "all";// Both options is same options. If use boolean in value, value will be ignored and only key will be inserted as options.
printFunction
This callback function is optional. It will be called if optipng will print something on stdout or stderr.
;
return
output = data: output file stdout: output string stderr: error string;
Full Example
Node.js
$ npm i -S optipng-js
var optipng = ;var fs = ; var input = fs;var output = ;// var output = optipng(input, {"o2": true});/* output = { data: output file, stdout: output string, stderr: error string }*/ console;console; fs;
Browser
Please check Demo with Web worker. https://li-na.github.io/optipng.js/
Build
Actually, I don't know what it is but I made build shell script and it seems working. Please let me know if you have ANY better way to build this project.
First, please setup emscript sdk on here. Then, download or clone this git and download optipng source code from website. Don't forget to extract optipng on ./deps/optipng
. Finally, just run ./build.sh
on Linux. It will configure optipng and compile with emcc.