Zero-Upload 300-DPI Photo Print Checker in the Browser: Magic-Byte Sniffing, Limiting-Edge Math, and 8x10 Aspect-Ratio Crop Traps

2026-12-05来源 dev.toEnglish

When people upload an old family scan to prepare an 8x10 framed gift, two technical problems account for almost every ruined print:

  1. Insufficient pixels for the target frame (sending a 900x1200 phone crop to an 11x14 printer and getting soft, blocky faces).
  2. Silent aspect-ratio cropping (sending a 2:3 snapshot to a 4:5 8x10 print counter that auto-zooms and trims ~17% off the forehead and chin).

While building the free 300-DPI Photo Print Calculator for Mended Photo, we decided the inspection step should run 100% client-side with zero bytes uploaded. Nobody wants to upload a 40 MB flatbed TIFF or private family portrait just to find out whether it is sharp enough for an 8x10 frame.

Here is how we built it in TypeScript and Svelte 5, including the edge cases that broke naive browser implementations.

1. Never trust file.type or file extensions

Desktop scanner software on Windows and older macOS machines routinely outputs files with empty file.type strings, mismatched extensions (a JPEG renamed .png, or a TIFF saved without a MIME hint), or scanned multi-page PDFs.

Instead of branching on file.type, we read only the first 16 bytes with file.slice(0, 16).arrayBuffer() and inspect magic bytes:

export function sniffMagicBytes(bytes: Uint8Array): SniffedFileKind { if (bytes.length < 4) return 'unknown'; if (bytes.length >= 5 && bytes[0] === 0x25 && bytes[1] === 0x50 && bytes[2] === 0x44 && bytes[3] === 0x46 && bytes[4] === 0x2d) { return 'pdf'; // %PDF- } if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return 'jpeg'; if (bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) return 'png'; if ( (bytes[0] === 0x49 && bytes[1] === 0x49 && bytes[2] === 0x2a && bytes[3] === 0x00) || (bytes[0] === 0x4d && bytes[1] === 0x4d && bytes[2] === 0x00 && bytes[3] === 0x2a) ) { return 'tiff'; // Little-endian II*\0 or big-endian MM\0* } if ( bytes.length >= 12 && bytes[4] === 0x66 && bytes[5] === 0x74 && bytes[6] === 0x79 && bytes[7] === 0x70 ) { const brand = String.fromCharCode(bytes[8], bytes[9], bytes[10], bytes[11]); if (['heic', 'heix', 'hevc', 'hevx', 'heim', 'heis', 'mif1', 'msf1'].includes(brand)) { return 'heic'; } } return 'unknown'; } 

That 16-byte slice costs almost zero memory even on a 50 MB archival scan and routes each file cleanly to the right browser decoder: native createImageBitmap for JPEG/PNG/WebP, an IFD parser for TIFF, HEIC decoding for iPhone photos, or XObject image extraction for flatbed scanner PDFs.

2. Limiting-edge DPI math (not naive diagonal or long-edge math)

A common mistake in online DPI calculators is dividing only the width by the print width, or averaging both axes. That breaks whenever the photo orientation does not match the print table, or when a 2:3 scan is printed onto a wider 4:5 frame.

To compute effective print DPI without stretching, normalize both the photo and the print frame into (shortEdge, longEdge) and take the minimum density across the two edges:

const shortPx = Math.min(inspected.width, inspected.height); const longPx = Math.max(inspected.width, inspected.height); const printGrades = PRINT_SIZES.map((sz) => { const dpiShort = shortPx / sz.shortIn; const dpiLong = longPx / sz.longIn; const effectiveDpi = Math.floor(Math.min(dpiShort, dpiLong)); const grade = effectiveDpi >= 300 ? 'crisp' : effectiveDpi >= 200 ? 'acceptable' : 'upscale'; return { ...sz, effectiveDpi, grade }; }); 

Why Math.min(dpiShort, dpiLong)? Because when a photo lab fills a frame edge-to-edge without distorting faces (CSS object-fit: cover behavior), the limiting edge determines the true pixel pitch on paper.

For example, consider a standard 35mm scan at 1800 x 2700 px (exact 2:3 aspect ratio) printed at 8 x 10" (4:5 ratio):

  • dpiShort = 1800 / 8 = 225 DPI
  • dpiLong = 2700 / 10 = 270 DPI
  • True print sharpness when filled cleanly is 225 DPI, while roughly 16.7% of the 2700 px long edge gets trimmed off to fit the 4:5 paper shape.

3. Turning the diagnostic into a practical enlargement guide

Once a user sees their scan scores 140 DPI at 8x10, raw numbers alone are not enough. Pairing the live local measurement with concrete guidance on how to enlarge small original prints to 5x7, 8x10, and 11x14 and exact same-day retail print pixel specs turns an abstract pixel count into an immediate decision: print as-is, re-scan at 600 to 1200 DPI, or upscale to a 4K master first.

And because the entire inspection runs client-side with zero network transfer, it responds in milliseconds even on slow connections and keeps private family photographs strictly on the user's device until they choose to restore one.

If you want to test it on one of your own JPEG, PNG, TIFF, HEIC, or scanned PDF files, the tool is live at mendedphoto.com/photo-print-dpi-calculator.

原文:https://www.a8q9.cn/en/article/zero-upload-300-dpi-photo-print-checker-in-the-browser-magic-byte-sniffing-limit-5f52ea.html

相关文章