MozJPEG WebAssembly Codec
npm install @wasm-codecs/mozjpeg
Requirements:
- Node.js 10 or later
import encode from '@wasm-codecs/mozjpeg';
(async () => {
const encodedImage = await encode(image, inputInfo, encodeOptions);
})();
Returns a buffer containing the compressed image data.
A raw RGB image input buffer.
Those are required informations about the raw input image.
Channels specifies the amount of channels in the image: Buffer
param and defaults to 3
(RGB).
type InputInfo = {
width: number;
height: number;
channels?: number;
}
All encoding options are optional and fall back to the default values.
type EncodeOptions = {
quality?: number;
baseline?: boolean;
arithmetic?: boolean;
progressive?: boolean;
optimizeCoding?: boolean;
smoothing?: number;
colorSpace?: ColorSpace;
quantTable?: number;
trellisMultipass?: boolean;
trellisOptZero?: boolean;
trellisOptTable?: boolean;
trellisLoops?: number;
autoSubsample?: boolean;
chromaSubsample?: number;
separateChromaQuality?: boolean;
chromaQuality?: number;
}
import fs from 'fs';
import sharp from 'sharp';
import encode from '@wasm-codecs/mozjpeg';
(async () => {
// read input image and convert it to a raw buffer using sharp
const {
data,
info: { width, height, channels },
} = await sharp('in.jpg')
.raw()
.toBuffer({ resolveWithObject: true });
// encode the image using @wasm-codecs/mozjpeg
const output = await encode(data, { width, height, channels });
// save the image to the file system
fs.writeFileSync('out.jpg', output);
})();
Licensed under the MIT license.
© Copyright Cyril Wanner