mozjpeg.js is a port of MozJPEG to JavaScript, via Emscripten. MozJPEG is a JPEG encoder by Mozilla that creates smaller JPEGs at the expense of longer encoding times. It exposes MozJPEG’s cjpeg
command functionality as a JavaScript function, and can be used from both within the browser and Node.js.
Example
const mozjpeg = require("mozjpeg-js");
const fs = require("fs");
const input = fs.readFileSync("in.ppm");
const out = mozjpeg.encode(input, { quality: 85 });
// out = { data: <mozjpeg output>, stderr: <cjpeg stderr> }
console.error(out.stderr);
fs.writeFileSync("out.jpg", out.data);
mozjpeg.encode
accepts a typed array of data and an object containing options to pass to cjpeg. It returns an object containing the output JPEG in data
as a Uint8Array
and a string containing stderr in stderr
.