Skip to content

Image Formats

bun-image-turbo supports a wide range of image formats for both reading and writing.

Format Support Matrix

FormatReadWriteNotes
JPEGTurboJPEG with SIMD acceleration
PNGAdaptive compression
WebPLossy and lossless modes
HEIC/HEIFmacOS ARM64 only
AVIFVia libheif
GIFAnimated support
BMPWindows bitmap
TIFFMulti-page support
ICOMulti-size icons

JPEG

High-quality lossy compression using TurboJPEG with SIMD acceleration.

typescript
import { toJpeg } from 'bun-image-turbo';

const jpeg = await toJpeg(buffer, {
  quality: 85  // 1-100, default: 80
});

Quality Guidelines

QualityUse CaseFile Size
95-100Archival, printLarge
80-90Web, high qualityMedium
60-75Thumbnails, previewsSmall
< 60Maximum compressionVery small

PNG

Lossless compression with configurable compression level.

typescript
import { toPng } from 'bun-image-turbo';

const png = await toPng(buffer, {
  compression: 6  // 0-9, default: 6
});

Compression Levels

LevelSpeedFile Size
0-2FastLarger
3-5BalancedMedium
6-9SlowSmallest

WebP

Modern format with excellent compression for both lossy and lossless.

typescript
import { toWebp } from 'bun-image-turbo';

// Lossy (default)
const lossy = await toWebp(buffer, {
  quality: 80  // 1-100
});

// Lossless
const lossless = await toWebp(buffer, {
  lossless: true
});

When to Use WebP

  • Lossy: General web images, photos
  • Lossless: Graphics, screenshots, images with text

TIP

WebP typically produces 25-35% smaller files than JPEG at equivalent quality.

HEIC/HEIF

Apple's modern image format, used by iPhones. Read-only support on macOS ARM64.

typescript
import { metadata, toJpeg } from 'bun-image-turbo';

// Read HEIC metadata
const info = await metadata(heicBuffer);
// { format: 'heic', width: 4032, height: 3024, ... }

// Convert to JPEG
const jpeg = await toJpeg(heicBuffer, { quality: 90 });

// Convert to WebP
const webp = await toWebp(heicBuffer, { quality: 85 });

Platform Limitation

HEIC support requires macOS ARM64 (M1/M2/M3/M4/M5). On other platforms, HEIC files will throw an error.

AVIF

AV1 Image Format - newer format with excellent compression. Read-only.

typescript
// Read AVIF files
const info = await metadata(avifBuffer);
// { format: 'avif', ... }

// Convert to other formats
const jpeg = await toJpeg(avifBuffer);

GIF

Supports both static and animated GIFs for reading. GIF output is available through transform().

typescript
import { metadata, transform } from 'bun-image-turbo';

// Read GIF metadata
const info = await metadata(gifBuffer);
// { format: 'gif', pages: 10, delay: [100, 100, ...], ... }

// Convert to GIF (via transform)
const gif = await transform(buffer, {
  output: { format: 'gif' }
});

Animated GIFs

When reading animated GIFs, metadata includes frame count (pages), loop count, and frame delays.

Using transform() with Formats

The transform() function accepts output format options:

typescript
import { transform } from 'bun-image-turbo';

// JPEG output
const jpeg = await transform(buffer, {
  resize: { width: 800 },
  output: {
    format: 'jpeg',
    jpeg: { quality: 85 }
  }
});

// PNG output
const png = await transform(buffer, {
  resize: { width: 800 },
  output: {
    format: 'png',
    png: { compression: 6 }
  }
});

// WebP output
const webp = await transform(buffer, {
  resize: { width: 800 },
  output: {
    format: 'webp',
    webp: { quality: 80, lossless: false }
  }
});

Format Detection

The metadata() function automatically detects image format:

typescript
const info = await metadata(buffer);

switch (info.format) {
  case 'jpeg':
  case 'png':
  case 'webp':
  case 'gif':
  case 'heic':
  case 'avif':
    // Process based on format
    break;
}