{
  "id": 18823307,
  "title": "Hitting an exact file size when compressing images in the browser",
  "url": "https://urgent.news/2026/11/08/hitting-an-exact-file-size-when-compressing-images-in-the-browser",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-08T00:23:24.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/sad1247/hitting-an-exact-file-size-when-compressing-images-in-the-browser-3af5"
  },
  "original_language": "en",
  "account": "Hitting the precise file size when compressing images in a browser is a challenge many websites face. Indian, South African, French, Canadian, and other portals each have their own maximum file size limits for uploaded photos, ranging from 50 KB to 10 MB. While these platforms don't compress files for users, they reject submissions that exceed the limit, requiring individuals to find a solution.\n\nA typical smartphone photo ranges from 3 to 5 MB in size, so reducing it to, say, 50 KB requires significant compression. A naive approach would be to simply set a JPEG quality level, but this doesn't work well. JPEG quality is a quantization parameter and the output size depends on the image content like noise, texture, and detail, not the quality value itself. So, finding the right quality setting to achieve the target file size is not straightforward.\n\nBinary search offers a solution. By encoding images repeatedly with different quality levels and checking if the output size matches the target, this method can efficiently narrow down the appropriate quality setting. A seven-round binary search narrows down the range of quality values to about 0.005, which is beyond human perception and takes less than a second on a phone.\n\nThe encoding process involves creating a canvas element, scaling it to fit the original image, drawing the image on the canvas, and then converting it to a blob (binary large object) with the desired quality. The initial canvas is filled with white to avoid black squares in the output JPEG due to the lack of an alpha channel in JPEG images. After encoding, dimensions of the canvas are set to zero to release the memory immediately.\n\nIf a single quality level isn't sufficient to reach the target file size, the algorithm then reduces the image resolution step by step while keeping the same quality level. This prioritizes quality over resolution, ensuring the image remains legible even at smaller sizes.",
  "summary": "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.…",
  "key_points": [
    "Different countries have varying maximum file size limits for uploaded photos.",
    "JPEG quality setting doesn't directly correlate with file size.",
    "Binary search efficiently finds the right JPEG quality for target file size."
  ],
  "editors_take": "Automating the compression process through binary search allows web users to bypass rigid upload limits by balancing image quality against specific file size constraints without manual trial and error.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}