Menu

Hitting an exact file size when compressing images in the browser
πŸ“°

Hitting an exact file size when compressing images in the browser

DEV Community: javascriptΒ·Sad1247Β·8 days ago
#wBpKKHn1
#devtojavascript#devto#dev#quality#size
Reading 0:00
15s threshold

India's chartered accountancy board caps the photo you upload with your exam form at 50 KB. South Africa's tax service wants supporting documents under 2 MB. A French rental platform stops at 10 MB per file. Canada's immigration portal has its own number.

None of these portals will compress your file for you. They reject it and let you work it out.

A photo from a modern phone is 3 to 5 MB. Getting it to 50 KB means dividing its weight by about eighty, and the obvious approach β€” pick a JPEG quality, encode, hope β€” doesn't survive contact with real files. Here's what actually works, and why the naive version fails.

Quality is not a dial you can read

The temptation is to map file size onto quality: 50 KB is small, so use quality 0.3.

That fails because JPEG quality isn't a size setting. It's a quantization parameter, and how many bytes it produces depends entirely on the image. A photo of a blank wall at quality 0.9 can be smaller than a photo of a forest at quality 0.4.…

Continue reading β€” create a free account

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