Most online image compressors upload your files to a server, process them there, and hand back a download. That is an odd amount of trust to extend for something your browser can already do.
Every current browser ships a complete image pipeline: decode, resize, re-encode, all locally. Here is how that works and where its limits are.
The three steps
Decode. createImageBitmap(file) turns a File into a bitmap, using the browser's own decoders — the same battle-tested code that renders every image you see.
Resize. Draw the bitmap onto a canvas at the target size:
const bitmap = await createImageBitmap(file);
const canvas = document.createElement("canvas");
canvas.width = 1920;
canvas.height = Math.round(bitmap.height * (1920 / bitmap.width));
const ctx = canvas.getContext("2d");
ctx.imageSmoothingQuality = "high";
ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
That imageSmoothingQuality line matters. The default produces noticeably worse downscaling.
Encode. canvas.toBlob(callback, "image/webp", 0.8) gives you a compressed blob to download or zip.
Resize first, then choose quality
Dimensions matter more than the quality slider. Halving the width and height of an image leaves a quarter of the pixels. A 4000 × 3000 phone photo scaled to 1920 pixels wide becomes 1920 × 1440, which is about 23% of the original pixel count before any compression is applied.
Pick the size from where the image will be displayed. A content column that is 720 CSS pixels wide needs about 1440 pixels on a 2× display, and anything larger is wasted bytes. Serving two sizes with srcset lets small screens download the small file:
<img src="photo-1440.webp" width="1440" height="960" alt="A short description"
srcset="photo-720.webp 720w, photo-1440.webp 1440w"
sizes="(min-width: 800px) 720px, 100vw">
Keep width and height in the markup so the browser reserves the space and the page does not jump as the image arrives. Add loading="lazy" for images below the fold, but not for the main image at the top of the page, which should start loading immediately.
Choosing a quality setting
Quality is a number between 0 and 1, and the useful range is narrower than people expect.
| Setting | Use for |
|---|---|
| 0.9–1.0 | Almost never — huge files, no visible gain over 0.85 |
| 0.75–0.85 | The sweet spot for photographs |
| 0.6–0.75 | Thumbnails, backgrounds, anything behind an overlay |
| Below 0.6 | Visible artefacts on most images |
For photographs, 0.8 typically gives a 40–80% reduction with no difference you can see at normal viewing size.
Flat graphics, screenshots and line art behave differently. Lossy formats produce ringing around hard edges, and PNG often beats JPEG outright on both size and quality. Compress photographs; leave diagrams as PNG.
Format, briefly
WebP is the safe default — roughly 25–35% smaller than JPEG at matching quality, supported everywhere that matters, and it keeps transparency.
AVIF goes further, often 20–30% below WebP, at the cost of slower encoding and slightly narrower support. Worth it for large hero images.
JPEG remains the maximum-compatibility choice, and has no alpha channel — transparent areas flatten to a background colour.
One trap: canvas.toBlob() silently falls back to PNG for a format the browser cannot encode. You get a file with the wrong extension and a mysteriously larger size. Probe support first by encoding a 1×1 canvas and checking blob.type matches what you asked for.
Where this approach breaks down
Very large images. Decoding a 100-megapixel file allocates a lot of memory. Browsers handle it, but a mobile device may not.
EXIF is dropped. Canvas gives you pixels, not metadata — orientation, camera settings and copyright tags do not survive. Usually a benefit for the web; a problem if you need provenance.
No progressive or advanced encoding. You get whatever the browser's encoder does. A dedicated tool like MozJPEG squeezes out a few more percent.
It is single-threaded by default. Compressing fifty images on the main thread will freeze the tab. Process them in sequence with yields, or move the work to a Web Worker.
Phone photos and orientation
Because EXIF is dropped, the output no longer carries a "rotate me" flag. Current browsers apply the orientation when they decode a photo, so the pixels you draw onto the canvas are already upright and the re-encoded file looks right. Do not simply assume it, though. Run one phone photo through before a whole batch, and check that it opens the right way up in more than one viewer.
Moving the work into a worker
For a batch, a Web Worker keeps the page responsive. OffscreenCanvas lets the worker draw and encode (with convertToBlob) without touching the page at all, and you post the finished blobs back one at a time. The code is the same three steps, just running somewhere the tab cannot be frozen.
A quick checklist before trusting a batch
- Compare one output with its original at 100% zoom. Look at edges, small text and smooth gradients, where artefacts show first.
- Confirm the format and the size. The file should be the format you asked for and smaller than the input. A "compressed" file that is larger usually means the encoder fell back to PNG, or the source was already well optimised.
- Check transparent images on a coloured background to see whether the alpha channel survived.
- Compare the total folder size before and after, rather than trusting any single image's percentage.
For the common case — a folder of photographs that need to be smaller before going on a website — none of these limits matter, and nothing ever leaves your machine. That is what our bulk image compressor does.
