Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How to Compress Images in the Browser Using JavaScript and the Canvas API

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…

Creating an Image Compression Tool with JavaScript and Canvas

In 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.

1. Build the HTML Interface

First, create an HTML file with the following elements:

- A label for selecting an image

- An input type="file" element to select an image (allowing JPEG, PNG, and WebP formats)

- A label for setting JPEG quality (0.1 to 1 in increments of 0.05)

- A compressed button

- A paragraph element to display result status

- A hidden anchor tag to download the compressed image

This interface will allow users to easily select an image, adjust the compression quality, and download the resulting compressed file.

2. Validate the Input File

Before 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.

3. Create the Image Compression Function

The 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.

Next, 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.

Finally, 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.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

GitHub Actions larger runners pricing 2026: 4-core and 8-core rates per minute and the break-even math

How much do GitHub Actions larger runners cost? Sengi Basic gives a GitHub Actions job 4 vCPU and 16 GB for $0.002 a minute, or 8 vCPU and 32 GB for $0.004, against $0.012 and $0.022 for GitHub’s…

  • GitHub offers $0.002/minute for 4-core and $0.004/minute for 8-core larger runners in 2026.
  • Sengi Basic provides 4 vCPU/16 GB for $0.002/minute and 8 vCPU/32 GB for $0.004/minute.

Giải mã Nemotron 3.5 Parse: Cách NVIDIA kết hợp 18,88 triệu mẫu tổng hợp, GRPO và DFlash2 để đạt 75,20 điểm ParseBench

Với các kỹ sư xây dựng pipeline RAG hay AI Agent, bước bóc tách tài liệu (document parsing) thường là nút thắt lớn nhất: mô hình nhỏ thì sai lệch bảng biểu, còn mô hình lớn lại quá chậm và tốn GPU.

  • NVIDIA mở toàn bộ công thức Nemotron 3.5 Parse trên Hugging Face
  • Mô hình 0,631 tỷ tham số được phát hành 17/10/2026 dưới giấy phép OpenMDW 1.1
  • Kết hợp SFT, RLVR và DFlash2 giúp mô hình đạt 75,20 điểm ParseBench

How to Speed Up Windows 11: Find the Real Cause First, Then Fix It

Most Windows 11 speed-up guides hand you the same 20-step list and hope something in it helps. The faster way is a two-minute check that tells you what is actually slow — then you fix only that thing.

  • Disk usage near 100% may indicate a full drive or struggling mechanical hard drive
  • Memory nearing full capacity often results from many startup apps or insufficient RAM

More from Thursday 26 November →