{
  "id": 21934268,
  "title": "How to Compress Images in the Browser Using JavaScript and the Canvas API",
  "url": "https://urgent.news/2026/11/26/how-to-compress-images-in-the-browser-using-javascript-and-the-canvas",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-26T06:07:41.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/nimzoa/how-to-compress-images-in-the-browser-using-javascript-and-the-canvas-api-5a41"
  },
  "original_language": "en",
  "account": "Creating an Image Compression Tool with JavaScript and Canvas\n\nIn this tutorial, we will create a browser-based image compression tool using JavaScript and the HTML Canvas API. The process involves selecting an image file, validating it, calculating safe output dimensions, and exporting the compressed JPEG file for download. Here's how to build this simple tool step-by-step.\n\n1. Build the HTML Interface\n\nFirst, create an HTML file with the following elements:\n\n- A label for selecting an image\n- An input type=\"file\" element to select an image (allowing JPEG, PNG, and WebP formats)\n- A label for setting JPEG quality (0.1 to 1 in increments of 0.05)\n- A compressed button\n- A paragraph element to display result status\n- A hidden anchor tag to download the compressed image\n\nThis interface will allow users to easily select an image, adjust the compression quality, and download the resulting compressed file.\n\n2. Validate the Input File\n\nBefore decoding the image, perform basic file validation to ensure the file is a supported image format, not empty, and within the configured size limit. The maximum file size is set to 25 MiB and the maximum pixel count to 25 million. The code also checks if the file is within these limits and rejects unsupported formats or invalid quality values.\n\n3. Create the Image Compression Function\n\nThe compression function is asynchronous and takes the file and compression quality as parameters. It starts by validating the file input and checking if the quality is a valid number between 0 and 1.\n\nNext, the function uses the Canvas API to decode the image into a bitmap. It checks if the image's dimensions are too large by comparing the product of width and height to the maximum allowed pixel count. If the image is too large, it calculates safe output dimensions by scaling the image down while maintaining its aspect ratio, ensuring the final dimensions do not exceed the maximum allowed width or height.\n\nFinally, the function creates a new canvas element to draw the compressed image, exports the canvas as a JPEG file, and provides a downloadable link for the compressed image result. This final step allows users to download the compressed image directly from the browser without uploading it to a server.",
  "summary": "Large images can increase page loading times, consume unnecessary bandwidth and negatively affect the user experience. But what if users could resize and compress images directly in their browsers without uploading files to a server? Using JavaScript and the HTML Canvas API, we can build a simple client-side image compressor without external libraries or paid APIs. In this tutorial, we will build…",
  "key_points": [],
  "editors_take": null,
  "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."
}