node-webp
Version:
WebP support for Node.js
67 lines • 2.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
/**
* Compresses an image using the WebP format. Input format can be either PNG, JPEG, TIFF, WebP or raw Y'CbCr samples.
* @param imageFilepath Path for the input image file.
* @param options Additional options for compression.
*/
async function compress(imageFilepath, options) {
const command = (0, util_1.getCommand)("cwebp");
const outputFilepath = (0, util_1.getOutputFilepath)(imageFilepath);
const args = [
imageFilepath,
"-o",
outputFilepath
];
if (options) {
const { quality, resize, crop, lossless, nearLossless, alphaQuality, preset, compressionLevel, multiThreaded, lowMemory } = options;
if (quality) {
args.push(`-q ${quality}`);
}
if (resize) {
args.push(`-resize ${resize.width} ${resize.height}`);
}
if (crop) {
args.push(`-crop ${crop.xPosition} ${crop.yPosition} ${crop.width} ${crop.height}`);
}
if (lossless) {
if (lossless.level) {
args.push(`-z ${lossless.level}`);
}
else {
args.push("-lossless");
if (lossless.preserveTransparency) {
args.push("-exact");
}
}
}
if (nearLossless) {
args.push(`-near_lossless ${nearLossless}`);
}
if (alphaQuality) {
args.push(`-alpha_q ${alphaQuality}`);
}
if (preset) {
args.push(`-preset ${preset}`);
}
if (compressionLevel) {
args.push(`-m ${compressionLevel}`);
}
if (multiThreaded) {
args.push("-mt");
}
if (lowMemory) {
args.push("-low_memory");
}
}
const { stderr } = await (0, util_1.execute)(command, args, {
shell: true
});
return {
log: stderr,
outputFilepath
};
}
exports.default = compress;
//# sourceMappingURL=compress.js.map
;