Choosing an image format: PNG, JPEG, WebP, AVIF, SVG, TIFF and BMP

How to pick the right image format for photographs, UI screenshots, transparent icons, vector illustrations, and print archival—and what edge cases to include in your image upload test plan.

1. Decision matrix by image content and delivery target

No single image format wins across every use case. Photographic camera images have continuous tone gradients and sensor noise that compress poorly in lossless dictionary coders, whereas flat-color UI graphics, diagrams, and text screenshots develop unsightly ringing artifacts when compressed with lossy 8×8 DCT blocks.

FormatCompressionTransparencyAnimationColor depthBest use case
JPEG (.jpg)Lossy DCTNoNo8-bit RGB / CMYKUniversal photography & email attachments
PNG (.png)Lossless DEFLATE8/16-bit RGBAVia APNG1/8/16-bit per channelScreenshots, UI assets, alpha cutouts, master graphics
WebP (.webp)Lossy VP8 & Lossless VP8L8-bit Alpha in both modesYes8-bit RGBA / YUVAll-purpose web delivery (25–35% smaller than JPEG/PNG)
AVIF (.avif)Lossy & Lossless AV18/10/12-bit AlphaYes8/10/12-bit HDR & SDRHigh-efficiency hero photos & HDR web imagery
SVG (.svg)XML Vector (gzip/brotli)Full RGBA & masksCSS / SMILResolution-independentLogos, icons, charts, diagrams at any DPI
TIFF (.tiff)Uncompressed, LZW, ZIP, JPEGYesMulti-page1 to 32-bit RGB/CMYKPrint publishing, multi-page document scans, GIS GeoTIFF
BMP (.bmp)Uncompressed DIB32-bit BGRANo1 to 32-bitZero-CPU-decode embedded bitmaps & Windows GDI tests

2. Security and parser pitfalls when accepting user-uploaded images

  • SVG is an executable XML document when opened directly: Unlike raster formats (PNG, JPEG, WebP, AVIF), an SVG file is an XML document that can contain inline <script> tags, onload event handlers, and external <image xlink:href (external URI)> references. If a user uploads an SVG and your server serves it from the same origin with Content-Type: image/svg+xml, opening that image URL directly in a browser tab executes any embedded JavaScript (Stored XSS). Always sanitize uploaded SVGs (e.g., with DOMPurify), serve user uploads from an isolated sandbox domain, or rasterize untrusted uploads to PNG/WebP.
  • Decompression bombs (pixel flood attacks): A solid-color 25000×25000 PNG compresses down to less than 150 KB on disk because DEFLATE compresses repeating zero rows at over 1000:1. However, decoding that image into a 32-bit RGBA bitmap allocates 25000 × 25000 × 4 = 2.5 GB of RAM. Always inspect the width and height from the 24-byte PNG IHDR chunk (bytes 16–23) before allocating a pixel buffer.
  • EXIF Orientation and CMYK color spaces: Test your image resizer against all 8 EXIF Orientation flags and our CMYK JPEG fixture on Media Edge Cases.

Related guides and test tools

Frequently asked questions

Why do JPEG images turn transparent PNG backgrounds black?
The JPEG specification (ITU-T T.81) only supports 1-channel grayscale, 3-channel YCbCr/RGB, and 4-channel CMYK/YCCK—it has no alpha channel. When a converter drops the RGBA alpha byte without compositing against white, transparent pixels with RGB=(0,0,0,0) become solid black RGB=(0,0,0).
How can I read a PNG image's width and height without decoding the file?
In every valid PNG file, the first chunk at byte offset 8 is IHDR. Bytes 16–19 store the width as a 32-bit big-endian unsigned integer, and bytes 20–23 store the height.