Fix: Could Not Transform Image in Astro

Error message:
Could not transform image.
Images & Assets 2025-01-25

What Causes This Error?

This error occurs when Astro’s image optimization service fails to process an image. Common causes include corrupted files, unsupported formats, or issues with the image service.

The Problem

---
import { Image } from 'astro:assets';
import brokenImage from '../assets/corrupted.jpg';
---

<!-- ❌ Image file is corrupted or malformed -->
<Image src={brokenImage} alt="Photo" />

The Fix

Replace Corrupted Image

# Verify image is valid
file src/assets/photo.jpg
# Should output: JPEG image data, ...

# Re-download or re-export the image

Use Original Format

---
import { Image } from 'astro:assets';
import photo from '../assets/photo.jpg';
---

<!-- ✅ Skip format conversion -->
<Image src={photo} alt="Photo" format="jpg" />

Common Scenarios

Corrupted Download

# Re-download the image
curl -O https://example.com/image.jpg

# Or use wget
wget https://example.com/image.jpg -O src/assets/image.jpg

Invalid File Extension

# Check actual format
file src/assets/image.jpg
# Output: PNG image data, ...

# ❌ File is PNG but named .jpg
# ✅ Rename to correct extension
mv src/assets/image.jpg src/assets/image.png

Re-export from Editor

1. Open image in editor (Photoshop, GIMP, etc.)
2. Export/Save As with fresh encoding
3. Replace the corrupted file

Disable Optimization for Problematic Image

---
// Skip Astro's image processing
import rawImageUrl from '../assets/problem.jpg?url';
---

<!-- ✅ Use raw URL without optimization -->
<img src={rawImageUrl} alt="Photo" width="800" height="600" />

Check Sharp Installation

# Astro uses Sharp for image processing
# Reinstall if corrupted
npm uninstall sharp
npm install sharp

# Or rebuild
npm rebuild sharp

Use Different Image Service

// astro.config.mjs
import { defineConfig, squooshImageService } from 'astro/config';

export default defineConfig({
  image: {
    // Try Squoosh instead of Sharp
    service: squooshImageService(),
  },
});

Memory Issues with Large Images

// astro.config.mjs
export default defineConfig({
  image: {
    service: {
      entrypoint: 'astro/assets/services/sharp',
      config: {
        limitInputPixels: false, // Allow larger images
      },
    },
  },
});

Remote Image Issues

---
import { Image } from 'astro:assets';
---

<!-- If remote image fails, check if URL is accessible -->
<Image
  src="https://example.com/image.jpg"
  alt="Photo"
  width={800}
  height={600}
/>

<!-- Try with inferSize to debug -->
<Image
  src="https://example.com/image.jpg"
  alt="Photo"
  inferSize
/>

Validate Image Files

// Script to check images
import sharp from 'sharp';
import { glob } from 'glob';

const images = glob.sync('src/assets/**/*.{jpg,png,webp}');

for (const image of images) {
  try {
    await sharp(image).metadata();
    console.log(`✓ ${image}`);
  } catch (error) {
    console.log(`✗ ${image}: ${error.message}`);
  }
}

Quick Checklist

  • Verify image opens in image viewer
  • Check file extension matches actual format
  • Re-download or re-export corrupted files
  • Try format="original" to skip conversion
  • Reinstall Sharp if build machine issues
  • Check remote image URLs are accessible