Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Decoding 50 MB Flatbed TIFFs and iPhone HEICs Client-Side in a Browser Photo Restoration Studio

When we launched Mended Photo for families restoring old prints at home, we assumed most uploads would be small JPEGs. Within 48 hours of real desktop users trying the studio, two edge cases showed up immediately: A vi

When we launched Mended Photo for families restoring old prints at home, we assumed most uploads would be small JPEGs. Within 48 hours of real desktop users trying the studio, two edge cases showed up immediately:

  1. A visitor uploaded a 50.4 MB uncompressed flatbed scanner .tiff from a 1200 DPI archival scan.
  2. Another visitor on Windows Chrome tried uploading a 1.47 MB iPhone .heic photo AirDropped or emailed from a relative's phone.

Browsers cannot natively decode .tiff in an <img> or <canvas> tag, and Chrome on Windows/Linux still does not decode .heic natively. Sending raw 50 MB TIFFs straight through a serverless Worker blows through request-size and memory limits.

Here is how we solved both cleanly in the browser — and why getting true 300-DPI print geometry right mattered just as much as image restoration itself.

1. Client-Side Flatbed TIFF Decoding (utif2) Without Hanging the Main Thread

Flatbed scanners (Epson Perfection, CanoScan, HP ScanJet) default to .tif/.tiff when users select archival 600–1200 DPI scans. They also produce multi-page TIFFs, 16-bit channels, and unusual photometric interpretations.

Instead of rejecting .tiff or forcing users to convert files in Preview/Photoshop first, we detect .tif/.tiff extensions and II*\0 / MM\0* magic headers on file drop, lazy-load utif2 only when a TIFF is actually dropped (so normal JPEG/PNG users pay zero bundle cost), and decode the largest IFD page straight to RGBA pixel data:

  • Decode IFDs via UTIF.decode(buffer) and select the primary full-resolution page (skipping embedded low-res scanner thumbnails).
  • Convert to RGBA (UTIF.toRGBA8(ifd)) and draw onto an offscreen canvas.
  • Downscale gracefully only when dimensions exceed our restoration pipeline ceiling while preserving full aspect ratio and EXIF orientation, then encode to high-quality JPEG/PNG before upload.

Result: real 50 MB+ desktop flatbed scans now decode locally in seconds with zero server-side TIFF memory spikes.

2. Desktop Windows/Chrome HEIC Decoding (heic-to) With Graceful Fallback

Nearly half of family photos start on an iPhone — someone snaps a picture of a framed portrait at Thanksgiving and emails it to their home PC. On macOS Safari, HEIC sometimes works; on Windows Chrome, createImageBitmap(file) throws immediately.

We added a targeted HEIC/HEIF branch:

  • Detect .heic/.heif by MIME type, file extension, and ISO Base Media File Format (ftypheic, ftypheix, ftypmif1) magic bytes so misnamed files are still caught.
  • Lazy-import heic-to on demand to transcode client-side to a clean JPEG blob before preview generation.
  • If a device still hits a corrupted or unsupported HEVC variant, fall back to a plain-English hint instead of a cryptic browser error.

3. Why Restoring Pixels Isn't Enough: True 300-DPI Framing & Print Crops

A restored image that looks sharp on a laptop screen often prints blurry or gets heads cropped off at Walgreens, CVS, or a local print counter because aspect ratios don't match standard US picture frames:

Frame Size Exact Ratio Pixels Needed at 300 DPI
4 × 6 in 2 : 3 1200 × 1800 px
5 × 7 in 5 : 7 1500 × 2100 px
8 × 10 in 4 : 5 2400 × 3000 px
11 × 14 in 11 : 14 3300 × 4200 px

To make framing predictable for non-technical users, we built two things:

  1. A free, zero-signup Photo Print DPI & Max Size Calculator that reads local image dimensions right in the browser (without uploading the photo) and shows maximum safe print sizes at 300 DPI vs. 200 DPI, plus crop percentages for 4×6, 5×7, 8×10, and 11×14 frames.
  2. Print-ready 300-DPI PDF exports directly inside the restoration studio alongside the full-resolution 4K master image, so families can upload once and pick up same-day prints locally without touching Photoshop.

Try It or Break It

If you have an old flatbed TIFF scan, a faded 1970s magenta print, or a creased black-and-white family portrait sitting on your drive, you can inspect a free watermarked preview on your own image before paying anything at mendedphoto.com (flat one-time packs starting at $4.99, no subscriptions).

Curious how others handle large client-side RAW/DNG or multi-page archival TIFF workflows in the browser — happy to swap notes in the comments.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.