How to Inject 300 DPI Losslessly Into JPEG & PNG Files in the Browser (JFIF APP0 & PNG pHYs)

2026-11-30来源 dev.toEnglish

Most web tools "convert to 300 DPI" by redrawing the image onto an HTML5 <canvas> and re-encoding it — which silently triggers JPEG lossy recompression and degrades print sharpness.

Instead, DPI in standard image files is purely a metadata header:

  • JPEG (JFIF APP0): Bytes inside the FF E0 marker segment define units (1 = dots per inch) and Xdensity / Ydensity as 16-bit big-endian integers (0x012C = 300).
  • PNG (pHYs chunk): Stores Pixels Per Unit in pixels per meter (Math.round(300 / 0.0254) = 11811) plus a recalculated CRC32 checksum.

By reading and patching these binary segments directly with JavaScript Uint8Array slices, you get true lossless 300 DPI output in milliseconds without touching a single pixel.

Live Tools Built With This Browser-First Approach

Happy to answer questions about binary chunk parsing in client-side JS!``

原文:https://www.a8q9.cn/en/article/how-to-inject-300-dpi-losslessly-into-jpeg-png-files-in-the-browser-jfif-app0-pn-dac186.html

相关文章