How to Optimize Images for Web Performance

Images are typically the largest contributor to page weight and load time. This guide covers optimizing them at the server/build level for meaningfully faster page loads.

Why Image Optimization Matters

Unoptimized images can be many times larger than necessary without any visible quality difference — directly translating to slower page loads, especially on mobile connections.

Choosing the Right Format

FormatBest For
WebPGeneral purpose, excellent compression, wide browser support
AVIFEven better compression than WebP, growing but not universal support
JPEGPhotos, fallback for older browsers
PNGImages requiring transparency, screenshots with text/UI
SVGIcons, logos, and other vector graphics

Converting Images to WebP (Command Line)

sudo apt install webp -y
cwebp -q 80 input.jpg -o output.webp

Batch Converting a Directory

for f in *.jpg; do cwebp -q 80 "$f" -o "${f%.jpg}.webp"; done

Converting to AVIF

sudo apt install libavif-bin -y
avifenc --min 20 --max 40 input.jpg output.avif

Serving Modern Formats with Fallback (Nginx + HTML)

<picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg" alt="Description">
</picture>

The browser automatically selects the first supported format, falling back to JPEG for older browsers — see How to Serve WebP/AVIF Images with Fallbacks in Nginx for a server-side automatic approach.

Resizing Images to Appropriate Dimensions

Never serve a 4000px-wide image when it displays at 400px — resize to the actual maximum display size:

sudo apt install imagemagick -y
convert input.jpg -resize 800x800\> output.jpg

\> only resizes if the image is larger than the target, never upscales.

Generating Responsive Image Sets

for size in 400 800 1200 1600; do
    convert input.jpg -resize ${size}x output-${size}w.jpg
done
<img
  srcset="output-400w.jpg 400w, output-800w.jpg 800w, output-1200w.jpg 1200w"
  sizes="(max-width: 600px) 400px, 800px"
  src="output-800w.jpg"
  alt="Description"
>

Lets the browser choose the most appropriately sized image for the visitor's actual screen/viewport.

Lazy Loading Below-the-Fold Images

<img src="image.jpg" loading="lazy" alt="Description">

Built into modern browsers — defers loading images not yet visible in the viewport, improving initial page load time.

Automating Optimization in a Build Pipeline

Integrate image optimization into your CI/CD build step (via a build-tool plugin or a dedicated image processing library) so every deployment automatically produces optimized images, rather than relying on manual conversion for every new asset.

Compressing Without Perceptible Quality Loss

Quality settings around 75-85 for WebP/JPEG typically produce minimal visible difference while significantly reducing file size — test specific images at different quality levels to find the right balance for your content.

Common Errors

Images look blurry after optimization — quality setting was too aggressive; increase it, or verify you're not resizing below the actual required display dimensions.

Best Practices

  • Always resize to actual display dimensions, never rely on CSS to shrink an oversized image
  • Serve modern formats (WebP/AVIF) with appropriate fallbacks
  • Lazy-load images below the fold

Continue Reading

Browse more articles in Static Site Hosting & Frontend Deployment.

  • image optimization, webp, avif, web performance images
  • 0 Корисниците го најдоа ова како корисно
Дали Ви помогна овој одговор?

Понудени резултати

How to Host a Static Website on a VPS with Nginx

Static websites — plain HTML, CSS, and JavaScript with no server-side processing —...

How to Deploy a Next.js Application on a VPS

Next.js supports several deployment modes — fully static export, server-side rendering with...

How to Deploy a Static Site Built with Astro, Hugo, or Jekyll

Static site generators (Astro, Hugo, Jekyll) produce plain HTML/CSS/JS at build time —...

How to Set Up a Jamstack Site with a Headless CMS Backend

The Jamstack architecture (JavaScript, APIs, Markup) combines a pre-built static frontend with a...

How to Configure Nginx for Single Page Applications (SPA Routing)

Single Page Applications (React, Vue, Angular) handle routing client-side in JavaScript —...