Image Compressor — Go 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 Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package imagecompressor ports the deterministic core of CosmoDev's
// image-compressor tool: the MIME-selection rule, the max-dimension resize
// math, and the binary search that finds the largest canvas quality (5..95)
// whose encoded size fits a byte budget.
//
// Language: Go (standard library only).
// CosmoDev polyglot showcase port of the `image-compressor` tool, ported from
// src/tools/ImageCompressor.tsx.
//
// The island's decode/draw/toBlob pipeline is canvas-bound
// (createImageBitmap -> drawImage -> canvas.toBlob(mime, q)), so it stays in
// the browser; the encoder plugs in here as an EncodeSize function, and the
// search over whole-point qualities is the portable part. The search probes
// at most seven qualities — enough to settle 5..95 — and reports not-found so
// the caller can re-encode once at the 5-point floor, exactly like the TSX.
//
// display source — part of CosmoDev's polyglot tool pages.
package imagecompressor
import "math"
// OutputMime returns the re-encode MIME type for a source MIME type: PNG
// sources re-encode as WebP (lossy pipeline); JPEG and WebP keep their format.
func OutputMime(fileType string) string {
if fileType == "image/jpeg" || fileType == "image/webp" {
return fileType
}
return "image/webp"
}
// TargetDimensions scales a source size down so no side exceeds maxDim,
// never up (maxDim <= 0 keeps the original size). Each side is clamped at
// 1 px and rounded half-up — math.Round matches JS Math.round on positive
// sizes — mirroring the TSX's Math.max(1, Math.round(side * scale)).
func TargetDimensions(width, height, maxDim int) (int, int) {
scale := 1.0
if maxDim > 0 {
scale = math.Min(1, float64(maxDim)/math.Max(float64(width), float64(height)))
}
px := func(v int) int {
return int(math.Max(1, math.Round(float64(v)*scale)))
}
return px(width), px(height)
}
// EncodeSize reports the encoded byte size of the current canvas at the given
// quality fraction (0.05..0.95) — the stand-in for canvas.toBlob(mime, q).
type EncodeSize func(quality float64) int
// CompressToBudget binary-searches canvas quality (5..95) until the encoded
// size fits the budget (targetKb * 1024) — the core of the TSX's target
// mode. Quality moves in whole points, mid = (lo+hi)/2, and at most seven
// probes run — enough to settle 5..95. It returns the best fitting quality;
// ok is false when nothing fits, and the caller then re-encodes once at the
// 5-point floor (the TSX fallback).
func CompressToBudget(encodeSize EncodeSize, targetKb int) (quality int, ok bool) {
budget := targetKb * 1024
lo, hi := 5, 95
for step := 0; step < 7 && lo <= hi; step++ {
mid := (lo + hi) / 2
if encodeSize(float64(mid)/100) <= budget {
quality = mid // fits — try higher quality
ok = true
lo = mid + 1
} else {
hi = mid - 1 // too big — step down
}
}
return quality, ok
}
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 →