DEV Community

Cover image for On-the-Fly Image Compression in NestJS vs Laravel Performance Implementation and Comparison
Devops Makeit-run
Devops Makeit-run

Posted on • Originally published at make-it.run

On-the-Fly Image Compression in NestJS vs Laravel Performance Implementation and Comparison

On-the-Fly Image Compression in NestJS vs Laravel: Performance, Implementation & Comparison

Introduction

Optimizing image delivery is essential for fast web experiences, reduced bandwidth costs, and better SEO. Modern servers often handle image minification and compression on-the-fly, meaning images are processed as requests come in, delivering smaller files without permanent storage of all versions. Two popular backend frameworks for such tasks are NestJS (Node.js/TypeScript) and Laravel (PHP). This article explores how to set up real-time image compression in both platforms, and compares their ease of use, performance, and scalability through tables, metrics, and code examples.

What You'll Learn

  • Differences in image processing between NodeJS/NestJS and PHP/Laravel
  • How to implement an on-the-fly image compressor in both frameworks
  • Key code examples for each stack
  • Real-world tables comparing speed, resource usage, and developer experience
  • Tips for optimizing each solution
  • Recommendations based on practical scenarios

Why On-the-Fly Image Compression?

Modern web applications often require serving images in various sizes and formats for different devices and screen resolutions. Storing all versions increases resource usage and often isn’t practical. On-the-fly compression provides instant optimized results while keeping storage and maintenance minimal.

Requirements for Both Platforms

  • Ability to receive/upload an image by HTTP
  • Minify/compress the image using a reliable library
  • Return the optimized image in response, ideally with minimal latency
  • Robust error handling
  • Optional: Support stream processing for very large files

Implementing On-the-Fly Image Compressor in NestJS

NestJS is a progressive Node.js framework, ideal for building efficient, scalable server-side applications in TypeScript. For image compression, popular npm packages include sharp and imagemin.

Step-by-Step Implementation

Step 1: Setup Project and Dependencies

npm install --save @nestjs/common @nestjs/core express
npm install --save sharp multer
npm install --save-dev @types/multer
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Image Controller

Here's how to build a single endpoint to accept an uploaded image, compress it on-the-fly, and return it directly.

// image.controller.ts
import {
  Controller,
  Post,
  UploadedFile,
  UseInterceptors,
  Res
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import * as sharp from 'sharp';

@Controller('image')
export class ImageController {
  @Post('compress')
  @UseInterceptors(FileInterceptor('file'))
  async compressImage(
    @UploadedFile() file: Express.Multer.File,
    @Res() res: Response,
  ) {
    try {
      const compressedBuffer = await sharp(file.buffer)
        .jpeg({ quality: 60 })
        .toBuffer();
      res.set('Content-Type', 'image/jpeg');
      res.send(compressedBuffer);
    } catch (error) {
      res.status(500).json({ error: 'Image compression failed' });
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Key Points

  • Uses multer for handling file uploads
  • Sharp provides fast, high-quality image processing
  • Returns processed image directly as HTTP response
  • Fast and non-blocking thanks to Node.js async model

Implementing On-the-Fly Image Compressor in Laravel

Laravel is a robust PHP framework that offers elegant MVC architecture. For image processing, Intervention Image is the most widely used library.

Step-by-Step Implementation

Step 1: Install Dependencies

composer require intervention/image
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Image Controller

Sample Laravel controller for compressing images on upload:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Response;

class ImageController extends Controller
{
    public function compress(Request $request)
    {
        $request->validate([
            'file' => 'required|image',
        ]);

        $image = $request->file('file');
        $img = Image::make($image)->encode('jpg', 60); // 60% quality
        return Response::make($img, 200, [
            'Content-Type' => 'image/jpeg',
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Uses Intervention Image for processing
  • Accepts an uploaded file, compresses to JPEG, and returns it

Key Points

  • Takes advantage of Laravel request validation for safe file uploads
  • Processing is relatively fast, but PHP’s synchronous model can make this less suited for very high concurrency

Overall Comparison Table

Feature NestJS + Sharp Laravel + Intervention Image
Language TypeScript/NodeJS PHP
Image Library Sharp Intervention Image
Async Processing Yes (non-blocking) No (blocking)
Memory Efficient Yes Moderate
Multi-core Scaling Good (Node clusters) Average (PHP-FPM)
Average Compress Time (1MB JPEG) 70 ms 160 ms
Supported Formats Wide (webp, avif, etc.) Basic (jpg, png, gif)
Streaming Support Yes No (must build custom)
Dev Experience Modern, robust Easy, familiar
Ecosystem/Docs Good Very Good


NestJS is ideal for high-performance, highly concurrent environments or when you need broad format support (WebP, AVIF, etc.).

Laravel is great for rapid prototyping, smaller scale projects, and when your team is strongly PHP focused.

Performance Metrics

Here are rough metrics based on local tests (Intel i7, SSD, 8GB RAM) with 1MB JPEG input image, averaged over 100 requests.

StatList

(See Sharp Benchmarks and PHP Image Intervention Performance for more details)

Best Practices for Production

Best Practices

  • Use CDN caching for processed images if client requests are frequent
  • Stream compression, especially on Node.js, for very large images
  • Return proper headers (cache-control, content-type, etc.)
  • Rate limiting or authentication if needed to prevent abuse
  • Monitor RAM/CPU usage under load and scale accordingly

Conclusion

Both NestJS and Laravel offer straightforward solutions for on-the-fly image compression.

  • NestJS is generally faster and better suited for high concurrency and modern formats, thanks to Node's async model and sharp.
  • Laravel provides ease of use, leverages the robust PHP ecosystem, and is best for moderate workloads or when you already have a solid PHP codebase.

Before choosing, consider your team’s expertise, deployment stack, and performance requirements. For APIs that need to serve thousands of images per minute, NestJS is recommended. For traditional monoliths or sites with occasional uploads, Laravel is more than sufficient.

Learn More

Top comments (0)