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.
Please use binary file like readFile on node or Uint8Array (converted from base64) on javascript.
// Node.js
var input = fs.readFileSync("input.png");
var output = optipng(input, ["-o2"]);
// Browser
function dataURLtoUint8(dataurl) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return u8arr;
}
function readFile (file, callback) {
var fileReader = new FileReader();
fileReader.onload = function() {
var ary = dataURLtoUint8(this.target.result);
callback(ary);
};
fileReader.readAsDataURL(file);
}
var input, output;
readFile(your_file_on_here, function(ary) {
input = ary;
output = optipng(input, ["-o2"]);
// do something with output
});
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.
This callback function is optional. It will be called if optipng will print something on stdout or stderr.
optipng(input, ["-o2"], function(str) {
console.log(str);
});
output = {
data: [output file],
stdout: [output string],
stderr: [error string]
};
$ npm i -S optipng-js
var optipng = require("optipng-js");
var fs = require("fs");
var input = fs.readFileSync("input.png");
var output = optipng(input, ["-o2"]);
// var output = optipng(input, {"o2": true});
/*
output = {
data: output file,
stdout: output string,
stderr: error string
}
*/
console.log(output.stdout);
console.log(output.stderr);
fs.writeFileSync("output.png", output.data);
Please check Demo with Web worker. https://li-na.github.io/optipng.js/
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.
You have to setup emscripten sdk on here first.
Then, download or clone this git on your linux computer. (Windows does not supported at this moment)
$ git clone https://github.com/LI-NA/optipng.js
Finally, just run ./build.sh
. It will configure optipng and compile with emcc.
Optipng source code is under zlib license