Image Compressor — JavaScript source
Shrink image file sizes entirely in your browser — fixed quality or an automatic fit to a size budget, optional max-dimension resize, batch support. Nothing uploads.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* image-compressor — re-encode images smaller, at a fixed quality or to fit a size budget.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `image-compressor` tool, ported from
* src/tools/ImageCompressor.tsx.
*
* The island's heavy lifting is canvas-bound (createImageBitmap -> drawImage
* -> canvas.toBlob(mime, q)), so it stays in the browser. What ports is the
* deterministic layer around it: the MIME-selection rule, the max-dimension
* resize math, and the binary search for the largest quality whose encoded
* size fits a byte budget. The encoder plugs in as a sizeAtQuality(q)
* function — the stand-in below is sized like a real one; real WebP/JPEG
* blob sizes from canvas.toBlob take its place at runtime.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
/** PNG sources re-encode as WebP (lossy pipeline); JPEG/WebP keep their format. */
export function outputMime(fileType) {
return fileType === 'image/jpeg' || fileType === 'image/webp' ? fileType : 'image/webp';
}
/**
* Scale a source size down so no side exceeds maxDim, never up (0 keeps the
* original size). Each side is clamped at 1 px, matching the live canvas code.
*/
export function targetDimensions(width, height, maxDim) {
const scale = maxDim > 0 ? Math.min(1, maxDim / Math.max(width, height)) : 1;
return [Math.max(1, Math.round(width * scale)), Math.max(1, Math.round(height * scale))];
}
/**
* Binary-search the canvas quality (5..95) until the encoded size fits the
* budget (targetKb * 1024) — the core of the island's target mode. Quality
* moves in whole points, mid = floor((lo + hi) / 2), and the loop runs at
* most seven times — enough to settle 5..95. Returns the best fitting
* quality, or null when nothing fits (the island then re-encodes once at the
* 5-point floor).
*/
export function compressToBudget(encodeSize, targetKb) {
const budget = targetKb * 1024;
let lo = 5;
let hi = 95;
let best = null;
for (let step = 0; step < 7 && lo <= hi; step++) {
const mid = Math.floor((lo + hi) / 2);
if (encodeSize(mid / 100) <= budget) {
best = mid; // fits — try higher quality
lo = mid + 1;
} else {
hi = mid - 1; // too big — step down
}
}
return best;
}
// Deterministic stand-in codec: size grows ~linearly with quality
// (~100 KB at q = 1.0), standing in for canvas.toBlob blob sizes.
const fakeEncodeSize = (q) => Math.round(12000 + 88000 * q);
// The live tool decodes the bitmap, draws it to a canvas, then searches.
// 50 KB budget: a mid-range quality fits just under it.
const q = compressToBudget(fakeEncodeSize, 50);
console.log(
q === null
? 'no quality fits — falling back to the 5-point floor'
: `best quality ${q} -> ${fakeEncodeSize(q / 100)} bytes`,
);
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →