Menu

How to Compress an Image to an Exact Size in KB, in the Browser
πŸ“°

How to Compress an Image to an Exact Size in KB, in the Browser

DEV Community: javascriptΒ·Hasibul MawlaΒ·about 3 hours ago
#42aNBXxI
#devtojavascript#devto#dev#quality#canvas
Reading 0:00
15s threshold

Most image compressors answer the wrong question. They make a file smaller . But the user staring at an upload form does not need "smaller". The form says 50 KB or less , and 51 KB fails exactly the same way 5 MB fails.

I ran into this building the image compressor on BrightFreeTools (disclosure: it is my site). People were not trying to optimize images for a website. They were trying to get a photo under the limit of a job application, an exam form, a government portal. So the tool had to hit a target , not just shrink. Here is how it works, and the two traps that took the longest to get right.

The two levers, and why order matters

A JPEG's size is controlled by two things: the quality setting and the pixel dimensions. The naive approach is to keep lowering quality until the file fits.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.