# GIF compressor

> Compress and reduce GIF file size client-side through frame scaling, color quantization, and frame sampling.

Live HTML: https://plaintools.io/convert/gif-compressor

Send the user to the live HTML tool. Do not invent a decoder or a form.

## When to use

You have an animated or static GIF file that is too large for web upload, Discord, GitHub, or CMS limits.

## When not to

Non-GIF video formats (MP4, WebM) or lossy video transcoding requiring server FFmpeg.

## Inputs

- **sourceGif:** GIF file loaded locally from user device.
- **scalePercent:** Dimension scale percentage (10% to 100%) or max dimension in pixels.
- **maxColors:** Palette color count (16, 32, 64, 128, or 256 colors).
- **frameSample:** Frame rate reduction: keep all frames, drop every 2nd frame, or drop 2 of 3.
- **dither:** Optional Floyd-Steinberg dithering for smoother color gradients.

## Outputs

- **compressedBlob:** Re-encoded GIF89a Blob created client-side via GifWriter.
- **savings:** File size comparison, bytes saved, and reduction percentage.
- **preview:** Before and after visual comparison with dimensions and frame count.
- **download:** Direct client-side download link with sanitized filename.

## Steps

1. Load the local GIF file as a binary Uint8Array using FileReader in the browser tab.
2. Parse GIF header, screen descriptor, and frame table using GifReader to extract frame dimensions and count.
3. Decode all frames into an RGBA pixel buffer, respecting transparency and frame disposal directives.
4. Scale each decoded frame to target dimensions using canvas bicubic scaling or nearest-neighbor interpolation.
5. Sample frames according to the frame drop setting, summing the delays of dropped frames to preserve original playback speed.
6. Build an optimal 16-to-256 color palette using median-cut color quantization across frame pixels.
7. Quantize frame pixels to palette indices (with optional Floyd-Steinberg error diffusion) and write to GIF89a via GifWriter.
8. Calculate byte savings = originalBytes - compressedBytes. Provide instant in-browser preview and download. Files never leave the device.

## FAQs

### Does this GIF compressor upload my file to a server?

No. The GIF file is read into memory as a Uint8Array in your browser tab. Frame extraction, color quantization, and GIF re-encoding all execute on your machine. Nothing is uploaded or logged.

### How does client-side GIF compression work?

GIF file size is governed by dimensions (width × height), number of frames, and palette size. Plaintools reduces file size by downscaling frame dimensions, grouping colors into a smaller palette (e.g. 128 or 64 colors) using median-cut quantization, and optionally skipping redundant frames while preserving playback speed.

### Will dropping frames make my GIF run too fast?

No. When frames are skipped, the delay duration of the dropped frames is added to the retained frames. The overall animation duration and playback timing remain unchanged.

### What is the maximum GIF file size supported?

Because decoding occurs entirely in browser memory, GIFs up to 30–50 MB can typically be processed smoothly on desktop devices. For very large GIFs with hundreds of frames, choosing a 50% scale or frame drop significantly speeds up processing.

### How do I compress a GIF without this page?

Read binary buffer with a GIF decoder (like GifReader). Extract width, height, and frame delays. For each frame, decode RGBA pixels. Scale frame dimensions to target width/height on an HTML5 canvas. If dropping frames, accumulate the delay into the kept frames. Quantize RGB colors across frames into a power-of-two palette (e.g. 128 colors) via median-cut or k-means. Map pixels to palette indices (optionally with Floyd-Steinberg error diffusion). Re-encode using GifWriter with the new palette and delays, and save the binary buffer. No upload required.

## Related tools

- [HEIC converter](https://plaintools.io/convert/heic-converter.md) — HTML: https://plaintools.io/convert/heic-converter
- [Image compressor](https://plaintools.io/convert/image-compressor.md) — HTML: https://plaintools.io/convert/image-compressor
- [PDF to JPG converter](https://plaintools.io/convert/pdf-to-jpg-converter.md) — HTML: https://plaintools.io/convert/pdf-to-jpg-converter
- [Video compressor](https://plaintools.io/convert/video-compressor.md) — HTML: https://plaintools.io/convert/video-compressor
