Format

Node Browser

Convert images between formats with per-format quality and encoding options.

Generic method

.format(type: 'jpeg' | 'png' | 'webp' | 'avif', options?: FormatOptions): Pipeline
.format(type: 'jpeg' | 'png' | 'webp' | 'avif', options?: FormatOptions): Pipeline

Format shorthands

.jpeg(options?: JpegOptions): Pipeline
.png(options?: PngOptions): Pipeline
.webp(options?: WebpOptions): Pipeline
.avif(options?: AvifOptions): Pipeline
.jpeg(options?: JpegOptions): Pipeline
.png(options?: PngOptions): Pipeline
.webp(options?: WebpOptions): Pipeline
.avif(options?: AvifOptions): Pipeline

quality

.quality(value: number): Pipeline
.quality(value: number): Pipeline

Set output quality (1–100) without specifying a format. Applied to whatever format is active at output time. Use format-specific methods when you need fine-grained control.

await img('photo.jpg').quality(75).toBuffer()
await img('photo.jpg').quality(75).toBuffer()

JPEG

OptionTypeDefaultDescription
qualitynumber80Quality 1–100. Lower = smaller file.
progressivebooleanfalseProgressive (interlaced) JPEG — loads top-to-bottom in browsers.
mozjpegbooleanfalseUse MozJPEG encoder for better compression at same quality.
// Standard quality
await img('photo.jpg').jpeg({ quality: 85 }).toBuffer()

// Progressive JPEG for web
await img('photo.jpg').jpeg({ quality: 80, progressive: true }).toBuffer()

// Maximum compression with MozJPEG
await img('photo.jpg').jpeg({ quality: 75, mozjpeg: true }).toBuffer()
// Standard quality
await img('photo.jpg').jpeg({ quality: 85 }).toBuffer()

// Progressive JPEG for web
await img('photo.jpg').jpeg({ quality: 80, progressive: true }).toBuffer()

// Maximum compression with MozJPEG
await img('photo.jpg').jpeg({ quality: 75, mozjpeg: true }).toBuffer()

PNG

OptionTypeDefaultDescription
qualitynumber100Quality 1–100. Values below 100 enable lossy compression.
compressionLevelnumber6zlib compression level 0–9. Higher = smaller file, slower.
progressivebooleanfalseInterlaced PNG.
// Lossless PNG
await img('photo.jpg').png().toBuffer()

// Lossy PNG (smaller file)
await img('photo.jpg').png({ quality: 80 }).toBuffer()

// Maximum compression
await img('photo.jpg').png({ compressionLevel: 9 }).toBuffer()
// Lossless PNG
await img('photo.jpg').png().toBuffer()

// Lossy PNG (smaller file)
await img('photo.jpg').png({ quality: 80 }).toBuffer()

// Maximum compression
await img('photo.jpg').png({ compressionLevel: 9 }).toBuffer()

WebP

OptionTypeDefaultDescription
qualitynumber80Quality 1–100.
losslessbooleanfalseLossless WebP encoding.
effortnumber4Compression effort 0–6. Higher = smaller file, slower encode.
// Standard WebP
await img('photo.jpg').webp({ quality: 85 }).toBuffer()

// Lossless WebP (good for graphics/screenshots)
await img('photo.jpg').webp({ lossless: true }).toBuffer()

// Maximum compression
await img('photo.jpg').webp({ quality: 80, effort: 6 }).toBuffer()
// Standard WebP
await img('photo.jpg').webp({ quality: 85 }).toBuffer()

// Lossless WebP (good for graphics/screenshots)
await img('photo.jpg').webp({ lossless: true }).toBuffer()

// Maximum compression
await img('photo.jpg').webp({ quality: 80, effort: 6 }).toBuffer()

WebP typically achieves 25–35% smaller files than JPEG at equivalent visual quality. It's the recommended format for most web use cases.


AVIF

OptionTypeDefaultDescription
qualitynumber50Quality 1–100. AVIF quality scales differently — 50 is visually good.
effortnumber4Compression effort 0–9. Higher = smaller file, significantly slower.
losslessbooleanfalseLossless encoding.
// Standard AVIF
await img('photo.jpg').avif({ quality: 50 }).toBuffer()

// High quality
await img('photo.jpg').avif({ quality: 70, effort: 6 }).toBuffer()

// Maximum compression (slow)
await img('photo.jpg').avif({ quality: 40, effort: 9 }).toBuffer()
// Standard AVIF
await img('photo.jpg').avif({ quality: 50 }).toBuffer()

// High quality
await img('photo.jpg').avif({ quality: 70, effort: 6 }).toBuffer()

// Maximum compression (slow)
await img('photo.jpg').avif({ quality: 40, effort: 9 }).toBuffer()

AVIF achieves 40–50% smaller files than JPEG but encoding is significantly slower than WebP. Use it for static assets where build-time encoding is acceptable.


Format comparison

FormatBest forTypical savings vs JPEG
JPEGPhotos, no transparencybaseline
PNGGraphics, transparency, losslesslarger
WebPWeb images, photos + graphics25–35% smaller
AVIFMaximum compression, modern browsers40–50% smaller

Examples

// Convert to WebP at 85% quality
await img('photo.jpg').webp({ quality: 85 }).toBuffer()

// Convert PNG to AVIF
await img('graphic.png').avif({ quality: 60 }).toBuffer()

// Resize then encode
await img('photo.jpg')
  .resize(1200)
  .webp({ quality: 85, effort: 5 })
  .toBuffer()

// Generic format method
await img('photo.jpg').format('webp', { quality: 80 }).toBuffer()
// Convert to WebP at 85% quality
await img('photo.jpg').webp({ quality: 85 }).toBuffer()

// Convert PNG to AVIF
await img('graphic.png').avif({ quality: 60 }).toBuffer()

// Resize then encode
await img('photo.jpg')
  .resize(1200)
  .webp({ quality: 85, effort: 5 })
  .toBuffer()

// Generic format method
await img('photo.jpg').format('webp', { quality: 80 }).toBuffer()