@awesome-compressor/browser-compress-image
Version:
🚀 A powerful, lightweight browser image compression library with TypeScript support. Compress JPEG, PNG, GIF images with multiple output formats (Blob, File, Base64, ArrayBuffer) and zero dependencies.
34 lines (32 loc) • 1.4 kB
JavaScript
import { __esm, __export } from "./chunk-BaU5PcSi.js";
import Compressor from "compressorjs";
//#region src/tools/compressWithCompressorJS.ts
var compressWithCompressorJS_exports = {};
__export(compressWithCompressorJS_exports, { default: () => compressWithCompressorJS });
async function compressWithCompressorJS(file, options) {
const { quality, mode, targetWidth, targetHeight, maxWidth, maxHeight, preserveExif = false } = options;
if (!file.type.includes("jpeg") && !file.type.includes("jpg")) throw new Error("CompressorJS is optimized for JPEG files");
return new Promise((resolve, reject) => {
const compressorOptions = {
quality,
retainExif: preserveExif,
mimeType: file.type,
success: (compressedBlob) => {
const blob = compressedBlob;
if (blob.size >= file.size * .98) resolve(file);
else resolve(blob);
},
error: reject
};
if (mode === "keepQuality") {
if (targetWidth) compressorOptions.width = targetWidth;
if (targetHeight) compressorOptions.height = targetHeight;
if (maxWidth) compressorOptions.maxWidth = maxWidth;
if (maxHeight) compressorOptions.maxHeight = maxHeight;
}
new Compressor(file, compressorOptions);
});
}
var init_compressWithCompressorJS = __esm({ "src/tools/compressWithCompressorJS.ts"() {} });
//#endregion
export { compressWithCompressorJS, compressWithCompressorJS_exports, init_compressWithCompressorJS };