An exact-size tool should search encoded outputs and verify the final bytes. A JPEG quality value is not a file-size target.

Why quality 70 does not mean 70 KB

Codec quality controls influence detail retention, but file size also depends on dimensions, noise, texture, alpha, metadata, and format. Two photos at the same dimensions and quality can have very different byte counts.

The bounded search used here

The optimizer begins at the original dimensions and encodes candidate quality levels. A bounded binary search keeps the highest candidate under the cap. If even the minimum useful quality is too large, it tries a controlled sequence of smaller dimensions and repeats.

  • Preserve dimensions first.
  • Keep the highest viable quality at those dimensions.
  • Reduce pixels only when the current grid cannot meet the cap.
  • Decode and check the final output before reporting success.

Impossible and already-small cases

A detailed transparent PNG may not fit under 5 KB without becoming tiny or changing format. The tool should say so instead of silently wrecking it. If the source is already under the limit, the best compression is often no compression at all.