Image Compressor for TypeScript

Free online image compressor with TypeScript code examples

Working with image compressor in TypeScript? Our free online image compressor helps TypeScript developers format, validate, and process data instantly. Below you will find TypeScript code examples using sharp so you can achieve the same result programmatically in your own projects.

Try the Image Compressor Online

Use our free Image Compressor directly in your browser — no setup required.

Open Image Compressor

TypeScript Code Example

import sharp, { OutputInfo } from 'sharp';

interface CompressOptions {
  maxWidth: number;
  maxHeight: number;
  quality: number;
  format: 'jpeg' | 'webp' | 'png';
}

async function compressImage(
  input: string,
  output: string,
  options: CompressOptions
): Promise<OutputInfo> {
  let pipeline = sharp(input)
    .resize(options.maxWidth, options.maxHeight, {
      fit: 'inside',
      withoutEnlargement: true,
    });

  if (options.format === 'jpeg') pipeline = pipeline.jpeg({ quality: options.quality });
  else if (options.format === 'webp') pipeline = pipeline.webp({ quality: options.quality });

  return pipeline.toFile(output);
}

compressImage('input.jpg', 'output.webp', {
  maxWidth: 1920, maxHeight: 1080, quality: 80, format: 'webp'
}).then(info => console.log(info));

Quick Setup

Library: sharp
npm install sharp @types/sharp

TypeScript Tips & Best Practices

  • Install @types/sharp for TypeScript definitions
  • sharp pipeline methods are chainable and typed
  • Use OutputInfo type for the result metadata

Frequently Asked Questions

Image Compressor in Other Languages

More TypeScript Tools