Image API WebP output: smaller files at quality 76
Set format to webp and the image API returns WebP at the width and quality you choose. On two iPhone photos at 1600 px, WebP was 38% and 53% of the JPEG size.
Set format to webp in the options and the image API returns WebP at the width and quality you choose. On two iPhone HEIC photos at 1600 px and quality 76, the WebP files were 38% and 53% of the size of the JPEG files. Two photos are not a benchmark. The same quality number does not give the same picture, so check your own images before you switch.
WebP is usually the smaller format for images on a web page, and the API lets you pick it per call. This post shows the option, the numbers from our test, and the cases where we would keep JPEG.
The option
format takes jpeg, webp, png or auto. AVIF is not offered yet. The image worker does not have an AVIF encoder, so the API does not accept avif.
The optimize call takes a source and an options object:
{
"source": { "url": "https://example.com/photos/kitchen.heic" },
"options": { "width": 1600, "format": "webp", "quality": 76 }
}
The same options work in a batch, where they apply to every image. See batch image optimization: one bad file does not stop the batch.
What we measured
We ran the worker's processImage function on two HEIC samples from our test fixtures. Each run used width 1600 and quality 76. One run produced JPEG and one produced WebP. Before encoding, the function auto-orients the image, converts it to sRGB and strips the metadata, as it does for every call.
| Photo | HEIC input | JPEG output | WebP output | WebP as % of JPEG |
|---|---|---|---|---|
sample-iphone-2.heic |
2,333,446 bytes | 135,182 bytes | 51,892 bytes | 38% |
sample-iphone.heic |
2,504,910 bytes | 248,561 bytes | 130,648 bytes | 53% |
Both outputs are 1600 × 2133 pixels. They are portrait images.
What the numbers do not show
- Two photos, one quality setting. This is not a benchmark. The gap depends on the picture. These two differ by 15 points.
- No visual comparison. We did not score how the pictures look. JPEG and WebP use different encoders, so quality 76 in one is not the same look as quality 76 in the other.
- No browser check. The test did not check which browsers your users run. The MDN image formats page lists WebP support.
Where WebP fits
- Web pages. Use WebP for images that a browser loads. Keep a JPEG version if your stack cannot serve WebP to every reader.
- PDFs. Keep JPEG. The image filters in the PDF standard do not include WebP. The contract also says
progressivemust befalsewhen the image goes into a PDF. - Photo reports. If the output goes into a PDF, the same rule applies. See PDF compression for photo reports.
What we ran
processImageinworkers/image-worker/src/pipeline.ts, run on the two HEIC fixtures inworkers/image-worker/src/fixtures-heic. One run usedformat: "jpeg"and one usedformat: "webp", both at width 1600 and quality 76. We measured the output buffers. The measurement used one throwaway test file, which we deleted afterwards.- The existing worker test
outputs WebP when format=webppassed. - No live call. We did not call the live API for this article. Run your own photos through the endpoint before you change a production setting.
Before you switch
- Test about 20 real photos from your users, at the sizes you serve.
- Compare the pictures at the zoom your users see.
- Keep JPEG for anything that goes into a PDF.
- Compare file sizes on your own set, not on ours.
For the resize step, read resize images for the web with an image API. For phone photos, read HEIC to JPEG: orientation and colour checks.
To try it, start free. New accounts get 1 USD of credit.