UNPKG

@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.

45 lines (43 loc) • 1.46 kB
import { __esm, __export } from "./chunk-BaU5PcSi.js"; import gifsicle from "gifsicle-wasm-browser"; //#region src/tools/compressWithGifsicle.ts var compressWithGifsicle_exports = {}; __export(compressWithGifsicle_exports, { default: () => compressWithGifsicle }); async function compressWithGifsicle(file, options) { const { quality, mode, targetWidth, targetHeight, maxWidth, maxHeight } = options; if (!file.type.includes("gif")) throw new Error("Gifsicle is only for GIF files"); let command; if (mode === "keepSize") command = ` -O1 --lossy=${Math.round((1 - quality) * 100)} ${file.name} -o /out/${file.name} `; else { let resizeOption = ""; if (targetWidth && targetHeight) resizeOption = `--resize ${targetWidth}x${targetHeight}`; else if (maxWidth || maxHeight) { const maxSize = Math.min(maxWidth || 9999, maxHeight || 9999); resizeOption = `--resize-fit ${maxSize}x${maxSize}`; } command = ` -O1 ${resizeOption} ${file.name} -o /out/${file.name} `; } const result = await gifsicle.run({ input: [{ file, name: file.name }], command: [command] }); const compressedBlob = result[0]; if (compressedBlob.size >= file.size * .98) return file; return compressedBlob; } var init_compressWithGifsicle = __esm({ "src/tools/compressWithGifsicle.ts"() {} }); //#endregion export { compressWithGifsicle, compressWithGifsicle_exports, init_compressWithGifsicle };