AI • Science • Technology • Future

Side Projects

Compress a Photo to Exactly 50KB in Your Browser

Image from the original article
Featured image: original publisher image. Via original publisher.

Government portals, exam registrations, and visa forms frequently impose strict file size limits—such as a photo under 50KB or a signature under 20KB. These limits are not arbitrary; they serve practical and technical purposes. Large files can overwhelm server resources, leading to slow processing or system crashes. Additionally, storing large files increases the attack surface for potential data breaches. Source: dev.to.

Why Upload Limits Exist

These limits are designed to ensure system stability and security. For instance, a single 10MB photo uploaded during a high-traffic exam registration could cause a server to freeze or crash, disrupting thousands of users. Similarly, large files increase the risk of exploitation by malicious actors who target data storage and transmission pathways.

Most users today resolve this by uploading their documents to a third-party website and hoping the form accepts the result. This approach, while convenient, raises serious privacy concerns. Once a file is uploaded to an external server, it may be stored, scanned, or shared—potentially exposing sensitive personal information like passport photos or ID scans. Even if the site claims to delete files after processing, users have little control over how their data is handled or whether it is retained.

How FreeCompressJPG Works

The tool FreeCompressJPG enables users to compress an image to a specific size—such as 50KB—without ever sending it to a remote server. It operates entirely within the browser using plain JavaScript. No backend processing is required. The user’s file remains on their device, ensuring complete privacy and control.

Step-by-Step Process

  1. Decode the image: The tool reads the uploaded file and converts it into a bitmap for processing. This step allows the browser to manipulate the image without relying on external services.
  2. Draw the image on a canvas: A new canvas is created. The image is drawn onto it with a white background. This is critical because JPEG files do not support transparency. If a transparent PNG is drawn directly onto a JPEG canvas, the transparent areas will appear as black, distorting the original image.
  3. Binary search for quality: The tool tests different JPEG quality values between 0.4 and 0.95. It identifies the highest quality that still keeps the file under the target size. This process is efficient because it avoids trial-and-error. Instead, it uses a binary search algorithm to narrow down the optimal quality value with high precision—reducing a 0.55-wide range to about 0.004, which is imperceptible to the human eye.
  4. Resize if needed: If the image is too large—such as a 12-megapixel photo—its dimensions are reduced. The process repeats to find a balance between file size and visual clarity. This is necessary because even at high quality, a high-resolution image will exceed size limits. Reducing pixel dimensions significantly lowers the file size while preserving visual fidelity through adaptive compression.

File size increases with quality, but there is no universal formula to convert a quality value into a file size. The actual output depends entirely on the image content. A simple white-background signature at 0.9 quality might result in only 15KB, while a detailed facial photo at 0.5 quality could still be 120KB. This variability means that a one-size-fits-all compression strategy is not feasible.

Why the Browser?

Privacy is the primary reason. Passport photos and ID scans are highly sensitive. They should not be stored on any server—even temporarily. By processing images entirely in the browser, users avoid any data transfer to third-party servers, eliminating the risk of exposure or misuse.

Speed is another key advantage. There is no upload or download step. A batch of 20 photos can be processed faster than a server-based tool would take to upload the first one. This is especially valuable in time-sensitive scenarios such as visa applications or exam registrations, where users need to submit multiple documents quickly.

Cost is also a factor. Without a backend infrastructure, there are no subscription fees or daily usage limits. The tool remains free and accessible to everyone, regardless of location or internet connectivity.

Target Size: 50,000 Bytes

Some forms define a kilobyte as 1,000 bytes, while others use 1,024. This is slightly stricter than the minimum required, but it passes both types of form checks, making it a reliable solution for users across different systems.

Image from the original article
original publisher image. Via original publisher. · Source

Privacy and Transparency

Users can verify that no data is transmitted by opening browser DevTools and navigating to the Network tab. No image data is sent during processing. Once the page has fully loaded, users can even disable Wi-Fi, and the tool will continue to function normally. This demonstrates that no file leaves the device.

The tool does send an anonymous counter—only to track usage. It contains no file data, no filename, and no identifier. This allows the developer to understand which tools are being used without compromising user privacy.

Limitations and Feedback

Not all images compress well. Some may appear blurry or lose detail, especially when heavily reduced. This is particularly true for images with fine textures, such as skin or fabric, where compression can introduce visible artifacts.

Forms may still reject the output. This can occur due to format, aspect ratio, or metadata issues. For example, some forms require a specific image orientation or resolution that may not be preserved during compression.

The author invites feedback on edge cases—especially where results are unexpected or forms fail to accept the output. This helps improve future versions of the tool and ensures it meets real-world needs.

FreeCompressJPG is available in 17 languages. This reflects the global nature of form requirements and ensures accessibility for users across different regions and linguistic backgrounds.

For similar tools that work without uploads, see FileForge or a free seating chart tool.

Try it yourself: compress to 50KB, to 20KB, or to 100KB.

Sources & further reading

Featured image: original publisher image. Via original publisher. Image source