Convert HEIC to JPG in the browser (and why Chrome can't just open it)
iPhones save photos as HEIC by default. That's fine until someone tries to upload one to a website, attach it to a form, or open it on Windows. A lot of upload forms only accept JPG or PNG, and most browsers can't even display a HEIC file.
If you build anything that accepts user photos, you'll hit this. Here's what's going on and how to convert HEIC to JPG entirely in the browser, without sending the photo to a server.
Why the browser can't just open it
HEIC is an image stored in the HEIF container, compressed with HEVC (H.265). HEVC is a video codec with patent licensing attached, so most browsers don't ship a decoder for still images in that format.
In practice:
- Safari on recent Apple devices can display HEIC.
- Chrome and Firefox can't decode it natively.
An <img src="photo.heic"> just fails, and createImageBitmap() rejects. So you can't rely on the browser's built-in image pipeline. You need a decoder that runs in JavaScript or WebAssembly.
The approach: decode with WASM, encode with canvas
The usual pattern is:
- Read the file the user picked.
- Decode the HEIC with a WebAssembly build of libheif (libraries like
heic2anyandlibheif-jswrap it). - Draw the decoded pixels onto a
<canvas>. - Export the canvas as JPEG with
canvas.toBlob().
Steps 3 and 4 are plain browser APIs. Once you have raw RGBA pixels, encoding to JPG is easy:
// pixels: Uint8ClampedArray of RGBA, width/height from the decoder
function pixelsToJpeg(pixels, width, height, quality = 0.92) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// JPG has no transparency, so paint a background first
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, width, height);
const imageData = new ImageData(pixels, width, height);
// putImageData ignores the background, so draw via a temp canvas
const tmp = document.createElement('canvas');
tmp.width = width;
tmp.height = height;
tmp.getContext('2d').putImageData(imageData, 0, 0);
ctx.drawImage(tmp, 0, 0);
return new Promise((resolve) => canvas.toBlob(resolve, 'image/jpeg', quality));
}
Two details worth calling out:
putImageDatareplaces pixels, it doesn't blend them. If the source has transparency and youputImageDatastraight onto the final canvas, transparent areas become black in the JPG. Drawing through a temporary canvas withdrawImagelets it composite over your background color.- Quality around 0.9 is a good default. HEIC is already efficient, so the JPG will usually be larger than the original HEIC file. That's expected.
Things that won't survive the conversion
Be upfront with users about what changes:
- Live Photos. A HEIC can hold more than one image. A converter normally takes the primary still and drops the motion part.
- Metadata. Re-encoding through canvas does not carry over EXIF (location, camera, date). For privacy that's often a feature, but don't promise it's preserved.
- HDR and wide color. iPhone photos are often Display P3 and sometimes HDR. After going through a standard sRGB canvas, colors can look slightly different.
- Memory. A 48 MP photo is about 190 MB of raw RGBA. Converting a batch at once can crash a mobile tab, so process files one at a time and release canvases when done.
Why do it client-side at all?
You could upload the HEIC and convert it on the server with libheif or ImageMagick. But the photo is personal, the file is large, and the user is often on mobile data. Doing it in the browser means the image never leaves their device and there's no upload wait.
The trade-off is the WASM decoder size (a few hundred KB to a couple of MB), so load it lazily only when someone actually picks a HEIC file.
Try it
I built this into PixelKaro's HEIC to JPG converter. It converts iPhone photos to JPG or PNG on your device, nothing is uploaded, and it handles a batch of photos at once.
If you've dealt with HEIC uploads in your own app, I'd like to hear how you handled it: convert on the client, on the server, or just ask users to change their camera setting?
Comments
No comments yet. Start the discussion.