gifsicle-wasm
v1.96.4
Published
gifsicle compiled to WebAssembly — optimize GIFs in the browser
Maintainers
Readme
gifsicle-wasm
gifsicle compiled to WebAssembly with Emscripten. Optimize GIFs entirely in the browser.
Built from a version-controlled submodule of the original gifsicle source in public CI. Also available as a Python wheel for native use.
Install
npm install gifsicle-wasmUsage
The WASM module exposes gifsicle's CLI entry point (_run_gifsicle(argc, argv)) — you pass command-line arguments, same as invoking the gifsicle binary.
import createGifsicle from "gifsicle-wasm";
const mod = await createGifsicle();
// Write input GIF to Emscripten virtual filesystem
mod.FS.writeFile("/input.gif", new Uint8Array(gifBuffer));
// Build argv
const args = ["gifsicle", "-O2", "--lossy=80", "-o", "/output.gif", "/input.gif"];
const argv = mod._malloc((args.length + 1) * 4);
const ptrs = [];
for (let i = 0; i < args.length; i++) {
const p = mod.stringToNewUTF8(args[i]);
ptrs.push(p);
mod.setValue(argv + i * 4, p, "i32");
}
mod.setValue(argv + args.length * 4, 0, "i32");
// Run gifsicle
mod._run_gifsicle(args.length, argv);
// Clean up
ptrs.forEach((p) => mod._free(p));
mod._free(argv);
// Read output
const output = mod.FS.readFile("/output.gif");For a complete working example including error handling and Web Worker isolation, see gifsicle-worker.js in the agent-log-gif project.
Node.js
Works in Node.js too — pass wasmBinary to avoid fetch:
import { readFileSync } from "fs";
import createGifsicle from "gifsicle-wasm";
const wasmBinary = readFileSync("node_modules/gifsicle-wasm/dist/gifsicle.wasm");
const mod = await createGifsicle({ wasmBinary });Building WASM locally
Requires Emscripten, autoconf, and automake.
bash build.sh
# Output: dist/gifsicle.js + dist/gifsicle.wasm
# Smoke test (requires Node.js)
node test_wasm.mjsHow is this different from gifsicle-wasm-browser?
Provenance: gifsicle-bin's WASM is built from a version-controlled submodule of the original gifsicle source in public CI.
Invocation model: gifsicle-bin's WASM exposes the raw _run_gifsicle(argc, argv) gifsicle CLI entry point instead of wrapping as a library. See License for more on why we do that.
Credits
gifsicle by Eddie Kohler. WASM build approach based on Simon Willison's work.
License
GPL-2.0-only. See LICENSE.
The WASM module only provides the same command-line-argument interface as the gifsicle binary, and can be run isolated in a separate browser worker. See the FSF's Plugins and Proprietary Systems FAQs.
