Image Compressor: how it works and how to build one
Make JPG, PNG and WebP images smaller in your browser. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Drop one or more images, set the quality and output format, and compare the size before and after. WebP usually saves 25 to 35% over JPEG at the same quality. Images are processed on your device and never uploaded.
When to use it
- Faster websites
- Upload limits on forms
- Email attachments
How to use it
- Drop images or click to choose them.
- Set the quality and format.
- Download each result.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/image-compressor
How it works
The browser can re-encode an image: draw it on a canvas, then call canvas.toBlob with a format (JPEG, WebP) and a quality between 0 and 1. Lower quality throws away detail the eye barely sees, which shrinks the file a lot.
WebP is usually 25–35% smaller than JPEG at the same visual quality. Resizing big photos to the size they’re displayed at saves even more.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
compress.ts (ts)
export async function compress(file: File, quality = 0.75, type = "image/webp", maxSide = 2000) {
const img = await createImageBitmap(file);
const k = Math.min(1, maxSide / Math.max(img.width, img.height));
const canvas = new OffscreenCanvas(Math.round(img.width * k), Math.round(img.height * k));
canvas.getContext("2d")!.drawImage(img, 0, 0, canvas.width, canvas.height);
return canvas.convertToBlob({ type, quality }); // a Blob you can download or upload
}
Make it production-ready
- Show before/after sizes and a side-by-side preview.
- Process many files with a queue so the page stays responsive.
Questions
Are my images uploaded?
No. Compression uses your browser’s canvas, so files never leave your device.
What quality should I use?
70 to 80% is hard to tell apart from the original for photos.
Free, runs in your browser, nothing is uploaded.