HEIC to JPG API: Accept iPhone Photos in Your Web App

Sooner or later an iPhone user uploads a profile picture or a receipt to your app, and it shows up as a broken image. iPhones save photos as HEIC by default, and most browsers and many server image libraries cannot read it. The robust fix is to convert HEIC to JPG the moment it is uploaded. This guide does that with one call to the PrivConvert file conversion API.

Where HEIC files come from

When an iPhone uploads through a web form, Safari often converts the photo to JPG on the way out. Often, but not always: files picked from the Files app, sent from a Mac, synced from iCloud or forwarded through messaging apps keep their HEIC format. So any app that accepts photos will receive some HEIC files, and the share grows with your iPhone audience.

One request

curl -X POST https://privconvert.com/api/convert/heic-to-jpg \
  -H "Authorization: Bearer $PRIVCONVERT_API_KEY" \
  -F "file=@IMG_0420.HEIC" \
  -F "quality=85" \
  -o IMG_0420.jpg

The photo goes in a field called file; the JPG is the response body. quality runs from 10 to 100 and defaults to 90. For photos shown on the web, 80 to 85 is visually identical and noticeably smaller.

A 12 megapixel iPhone photo (1.8 MB HEIC) takes about 1.2 seconds to convert to a full resolution JPG on our side, plus the time to upload the photo and download the result.

Convert on upload

Detect HEIC by its bytes rather than by the file name or the browser's content type, both of which are often wrong (plenty of HEIC files arrive named .jpg). A HEIC file has a short brand code at bytes 8 to 12. Convert only those, and the rest of your code only ever sees normal images.

Python (FastAPI)

from fastapi import FastAPI, UploadFile
from privconvert import PrivConvert

app = FastAPI()
pc = PrivConvert()  # reads PRIVCONVERT_API_KEY

HEIC_BRANDS = (b"ftypheic", b"ftypheix", b"ftypmif1", b"ftypmsf1", b"ftyphevc")

@app.post("/avatar")
async def avatar(photo: UploadFile):
    data = await photo.read()
    if data[4:12] in HEIC_BRANDS:  # check the bytes, not the file name
        data = pc.convert("heic-to-jpg", data, filename=photo.filename, quality=85).content
    save_avatar(data)  # always a browser-friendly image from here on
    return {"ok": True}

Node.js (Express)

import express from "express";
import multer from "multer";
import { PrivConvert } from "privconvert";

const app = express();
const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 25e6 } });
const pc = new PrivConvert(); // reads PRIVCONVERT_API_KEY

const isHeic = (b) => ["heic", "heix", "mif1", "msf1", "hevc"].includes(b.toString("latin1", 8, 12));

app.post("/photos", upload.single("photo"), async (req, res) => {
  let image = req.file.buffer;
  if (isHeic(image)) {
    const result = await pc.convert("heic-to-jpg", image, {
      filename: req.file.originalname,
      params: { quality: 85 },
    });
    image = result.content;
  }
  await savePhoto(image);
  res.json({ ok: true });
});

PHP and Go work the same way with the official SDKs; see the SDK reference.

What you get back

  • Full resolution. The JPG has the same pixel dimensions as the photo. Resize afterwards if you need thumbnails.
  • Upright. The camera's orientation is applied to the pixels, so portrait photos are never sideways.
  • Correct colour. Wide-gamut iPhone colour is mapped to sRGB, so reds and skin tones look the way they did on the phone.
  • No location. GPS coordinates, camera serial numbers and capture times are not copied to the JPG. If you publish user photos, this protects your users from sharing their home address by accident. More on why in removing EXIF data before sharing photos.

A whole album at once

For imports and migrations, send up to 20 photos per request and get a ZIP of JPGs back:

# Up to 20 photos per request; the response is a ZIP of JPGs
from pathlib import Path

photos = sorted(Path("uploads").glob("*.HEIC"))[:20]
pc.convert_batch("heic-to-jpg", photos, quality=85).save("photos.zip")

In our measurements a batch of 10 iPhone photos (18 MB) took about 5.5 seconds to convert, plus upload and download time. For thousands of files, see converting thousands of files.

Errors worth handling

  • 400 bad request: the file is not an image at all, or the photo is truncated or damaged. The message says which; ask the user to upload it again. Other image formats sent by mistake (JPG, PNG, WEBP) are simply converted to JPG, so they never fail.
  • 429 rate limit: wait and retry. The SDKs already back off for you; see API retry strategy.

Photos are personal data

User photos show faces, homes and documents. PrivConvert converts each one in memory and returns the result in the same response: nothing is written to disk and no copy is kept. Your users' photos exist only in your storage.

Converting a few photos by hand? The free HEIC to JPG converter does the same in the browser, and how to convert HEIC to JPG covers the options on iPhone, Mac and Windows.

FAQ

Why can't I just show HEIC files in the browser?

Most browsers cannot decode HEIC. An iPhone photo uploaded as HEIC shows as a broken image for most of your visitors, and many image libraries on the server cannot read it either. Converting to JPG once, at upload time, removes the problem everywhere downstream.

Is the photo rotated correctly?

Yes. The orientation recorded by the camera is applied to the pixels, so the JPG is upright in every viewer, including ones that ignore orientation tags.

Is GPS location removed?

Yes. The JPG is written without the EXIF block, so the GPS position, camera serial and capture time from the original photo are not carried over. For user uploads that is usually what you want anyway.

Will colours change?

iPhones shoot in a wide colour space. The converter maps the colours to standard sRGB, the space browsers assume, so the JPG looks the same as the original on screen instead of slightly dull.

Can I get PNG instead of JPG?

Yes, use the heic-to-png tool with the same request. PNG is lossless but several times larger, so JPG is the better choice for photos.

Are the photos stored?

No. Each photo is converted in memory and the JPG is returned in the same response. Nothing is written to disk and no copy is kept.

Developer resources:
Official SDKs Error reference Plans & free trial HEIC to JPG online