home / skills / secondsky / claude-skills / image-optimization

This skill helps optimize images for web performance by selecting modern formats, responsive srcset, and lazy loading techniques.

npx playbooks add skill secondsky/claude-skills --skill image-optimization

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.9 KB
---
name: image-optimization
description: Optimizes images for web performance using modern formats, responsive techniques, and lazy loading strategies. Use when improving page load times, implementing responsive images, or preparing assets for production deployment.
license: MIT
---

# Image Optimization

Optimize images for web performance with modern formats and responsive techniques.

## Format Selection

| Format | Best For | Compression |
|--------|----------|-------------|
| JPEG | Photos | Lossy, 50-70% reduction |
| PNG | Icons, transparency | Lossless, 10-30% |
| WebP | Modern browsers | 25-35% better than JPEG |
| AVIF | Next-gen | 50% better than JPEG |
| SVG | Logos, icons | Vector, scalable |

## Responsive Images

```html
<picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <img
    src="image.jpg"
    srcset="image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w"
    sizes="(max-width: 600px) 100vw, 50vw"
    alt="Description"
    loading="lazy"
    decoding="async"
  >
</picture>
```

## Lazy Loading

```html
<!-- Native lazy loading -->
<img src="image.jpg" loading="lazy" alt="Description">

<!-- With blur placeholder -->
<img
  src="placeholder-blur.jpg"
  data-src="image.jpg"
  class="lazy"
  alt="Description"
>
```

## Build Pipeline (Sharp)

```javascript
const sharp = require('sharp');

async function optimizeImage(input, output) {
  await sharp(input)
    .resize(1200, null, { withoutEnlargement: true })
    .webp({ quality: 80 })
    .toFile(output);
}
```

## Performance Targets

| Asset Type | Target Size |
|------------|-------------|
| Hero image | <200KB |
| Thumbnail | <30KB |
| Total images | <500KB |

## Optimization Checklist

- [ ] Use WebP with JPEG fallback
- [ ] Implement responsive srcset
- [ ] Enable lazy loading for below-fold
- [ ] Compress at quality 70-85
- [ ] Serve from CDN
- [ ] Set proper cache headers

Overview

This skill optimizes images for web performance using modern formats, responsive techniques, and lazy loading strategies. I provide production-ready recommendations and code patterns to reduce payload, improve load times, and simplify integration with build pipelines. Use it to prepare assets for deployment and meet practical performance targets.

How this skill works

The skill inspects image assets and suggests format conversion (WebP, AVIF) with JPEG/PNG fallbacks, generates responsive srcset entries, and applies lazy loading where appropriate. It includes build-pipeline examples (Sharp) to produce resized and compressed outputs, plus guidance on placeholders, decoding, and cache headers. The result is a compact, responsive image set served from a CDN with correct headers.

When to use it

  • Reducing page load time and Largest Contentful Paint (LCP)
  • Implementing responsive images across breakpoints
  • Preparing image assets for production deployment
  • Switching to modern image formats (WebP/AVIF) with fallbacks
  • Adding lazy loading and placeholders for below-the-fold content

Best practices

  • Select format by use case: AVIF/WebP for photos, SVG for icons, PNG for transparency
  • Generate multiple sizes and use srcset + sizes for responsive delivery
  • Compress images at quality 70–85 and test visual impact
  • Enable native loading="lazy" and decoding="async" for non-critical images
  • Serve images via CDN and set long cache headers with immutable strategies

Example use cases

  • Create hero, thumbnail, and responsive variants during CI with Sharp
  • Add a <picture> block to serve AVIF/WebP with JPEG fallback
  • Implement blur-up placeholders for slow network conditions
  • Enforce asset size targets: hero <200KB, thumbnail <30KB, total images <500KB
  • Automate format conversion and resizing before deployment

FAQ

Which format should I choose by default?

Use WebP as the default for photos with AVIF for higher compression where supported; provide JPEG/PNG fallbacks for legacy clients.

How many sizes should I generate?

Generate at least 3 sizes (small, medium, large) matching your most common breakpoints; add more for high-density displays as needed.