Indian government exam portals have a rule that has quietly shaped a lot of my code: your photo must be under 50KB. Not "small". Not "optimised". Under 50KB, or the upload is rejected.

Every free tool I found for this wanted me to upload the photo to a server, wait in a queue, and create an account. For a file I just wanted to shrink. So I wrote it myself, in the browser.

This post is about the actual technique — hitting an exact byte target with canvas — and, honestly, about where my implementation still falls short.

The naive version, and why it fails

The obvious approach: