DEV Community

Cover image for Node.js Image Compression: A Complete API Integration Guide
Roman Klymenko
Roman Klymenko

Posted on • Originally published at pixozip.com

Node.js Image Compression: A Complete API Integration Guide

Tired of bloated images slowing down your app? Let's fix that with Node.js image compression.

Every KB counts. Uncompressed images can drain bandwidth and kill user experience. An effective image compression API solves this instantly—compress images up to 90% without quality loss.

Here's how to set up image compression in Node.js using a REST API:

const axios = require('axios');
const fs = require('fs');

async function compressImage(imagePath) {
 const imageBuffer = fs.readFileSync(imagePath);

 const response = await axios.post(
 'https://api.pixozip.com/compress',
 imageBuffer,
 {
 headers: {
 'Content-Type': 'image/jpeg'
 }
 }
 );

 fs.writeFileSync('compressed.jpg', response.data);
 console.log('Image compressed successfully!');
}

compressImage('./photo.jpg');
Enter fullscreen mode Exit fullscreen mode
  • 90% compression without visible quality loss
  • Free image compression API tier available
  • Works with image resize API features
  • Perfect for PHP image compression too
  • Acts as your image CDN alternative

Need batch processing? Use nodejs image compression with async/await:

const files = ['img1.jpg', 'img2.png', 'img3.webp'];
await Promise.all(files.map(compressImage));
Enter fullscreen mode Exit fullscreen mode
  • Cache compressed results—no need to re-compress

Top comments (0)