Forms for exams, visas and job portals often ask for a photo "at 300 DPI". Most people then ask the obvious question: what DPI is my photo right now? The usual answers are "open it in Photoshop" or "check Windows file properties". I wanted to answer it in the browser without decoding the image, so I wrote a small reader that looks at the header bytes only. This post walks through how it works.
DPI is metadata, not pixels
DPI tells a printer how large to print the pixels that already exist. A 1200 × 1800 photo prints at 4 × 6 inches at 300 DPI and at 16.7 × 25 inches at 72 DPI. Same pixels, same file, different print size. Changing 72 to 300 adds no detail at all.
That also means you never need to decode the image to read it. The value sits in a header, and an ArrayBuffer is enough.
Step 1: detect the format from magic bytes
Never trust the file extension or the MIME type the browser reports. Check the first bytes:
function isJpeg(b: Uint8Array) { return b.length > 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff } const PNG_SIG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a] function isPng(b: Uint8Array) { return b.length > 8 && PNG_SIG.every((v, i) => b[i] === v) } WebP is worth a special case: it has no standard density field, so the honest answer is "there is no DPI here", not a made-up 72.
Step 2: JPEG has two places for DPI
A JPEG can store density in the JFIF APP0 segment and also in the EXIF XResolution / YResolution tags. Programs disagree about which one wins (Photoshop and Windows Explorer read EXIF), which is why two tools can show different DPI for the same file.
Reading JFIF is a few bytes after the JFIF\0 identifier:
function readJfifDpi(b: Uint8Array, dataStart: number) { const units = b[dataStart + 7] const view = new DataView(b.buffer, b.byteOffset) const x = view.getUint16(dataStart + 8) const y = view.getUint16(dataStart + 10) if (units === 0 || x === 0 || y === 0) return null // 0 = aspect ratio only const k = units === 2 ? 2.54 : 1 // 2 = dots per cm return { x: Math.round(x * k), y: Math.round(y * k), source: "jfif" } } Two traps here:
-
units === 0means the numbers are only a pixel aspect ratio. A lot of files carry0, 1, 1. That is not "1 DPI", it is "no DPI". -
units === 2means dots per centimetre, so multiply by 2.54.
If JFIF has nothing usable, fall back to the EXIF IFD: find tags 0x011a and 0x011b (rationals) and 0x0128 (the unit). Remember EXIF can be big-endian or little-endian, so read the II / MM marker first.
Step 3: PNG stores it once, in pHYs
PNG keeps density in the pHYs chunk as pixels per metre, plus a unit byte:
const unit = b[phys.dataStart + 8] if (unit !== 1) return null // 0 = unknown unit, only a ratio const x = Math.round(view.getUint32(phys.dataStart) * 0.0254) const y = Math.round(view.getUint32(phys.dataStart + 4) * 0.0254) 2835 pixels per metre comes out as 72 DPI, and 11811 as 300. One more case I hit in practice: some PNGs written by libvips or Photoshop have no pHYs but declare resolution inside an eXIf chunk, so it is worth checking that too.
Step 4: say "no DPI" when there is none
Screenshots and images exported from a <canvas> usually have no density at all. Many tools quietly show 72 or 96 in that case, which sends people looking for a setting that does not exist. Returning { x: 0, y: 0, source: "none" } and saying "No DPI set" in the UI is more useful.
What I show the user
Once the DPI is known, the useful numbers are simple arithmetic:
- print size in inches:
width / dpibyheight / dpi - print size in cm: the same times 2.54
- the largest sharp print at 300 DPI:
width / 300byheight / 300
The last one is the number people actually need, because print quality depends on pixel count, not on the DPI field.
Try it
I put this into a free DPI checker: drop a JPG or PNG and it shows the DPI, which header it came from, and the print size. Nothing is uploaded. If a form needs a specific value, the DPI converter rewrites only the header (both JFIF and EXIF, so programs agree), and the pixel data stays byte-identical.
If you have a file where the JFIF and EXIF values disagree, I would like to see it.

