Image Compressor for JavaScript
Free online image compressor with JavaScript code examples
Working with image compressor in JavaScript? Our free online image compressor helps JavaScript developers format, validate, and process data instantly. Below you will find JavaScript 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 CompressorJavaScript Code Example
const sharp = require('sharp');
async function compressImage(input, output) {
const info = await sharp(input)
.resize(1920, 1080, { fit: 'inside', withoutEnlargement: true })
.jpeg({ quality: 80, progressive: true })
.toFile(output);
console.log(`Compressed: ${info.size} bytes (${info.width}x${info.height})`);
}
// WebP conversion for better compression
async function toWebP(input, output) {
await sharp(input)
.webp({ quality: 80 })
.toFile(output);
}
compressImage('input.jpg', 'output.jpg');Quick Setup
Library: sharp
npm install sharpJavaScript Tips & Best Practices
- sharp is the fastest Node.js image processing library
- WebP typically gives 30% smaller files than JPEG
- Use fit: 'inside' to resize without cropping