Students and job applicants in India routinely hit strict file-size rules when uploading passport photos and signatures to portals like SSC MTS, UPSC, CUET UG, IBPS, and NEET—often under 20KB, 50KB, or 100KB in JPEG format.
Most online compressors upload your personal ID images to a third-party server. Here is how to build—or use—a 100% browser-side compressor using the HTML5 Canvas API with binary search on JPEG quality.
Why Binary Search Beats Fixed Quality Compression
Setting a hardcoded JPEG quality like 0.7 fails unpredictably:
- A clean portrait photo might stay
95KB(rejected by a50KBform limit). - Or compress down to
14KBand fail readability checks.
Instead, running a 6-step binary search between quality = 0.1 and quality = 0.95 converges cleanly on the highest visual clarity that still stays below your target byte budget:
async function compressToTargetKB(canvas, targetKB) {
const maxBytes = targetKB * 1024;
let low = 0.08;
let high = 0.95;
let bestBlob = null;
for (let i = 0; i < 7; i++) {
const mid = (low + high) / 2;
const blob = await new Promise((resolve) =>
canvas.toBlob(resolve, "image/jpeg", mid)
);
if (blob.size <= maxBytes) {
bestBlob = blob;
low = mid; // try higher visual quality
} else {
high = mid; // shrink further
}
}
return bestBlob;
}
Live Zero-Upload Utility & Exam Guides
- Use the tool directly (zero server upload): Free 20KB / 50KB / 100KB Image Compressor
- SSC MTS Photo & Pattern Blueprint: SSC MTS Complete Exam & Form Guide
- CUET UG Document Rules: CUET UG Registration & Upload Checklist
- Explore all 29+ free utilities: Theory of Everything Portal
Top comments (0)