On-the-fly Image Compression In Nestjs Vs Laravel Performance...

On-the-fly Image Compression In Nestjs Vs Laravel Performance...

Posted on Nov 17

• Originally published at make-it.run

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.

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.

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.

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

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

Sample Laravel controller for compressing images on upload:

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.

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

Source: Dev.to