🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Performance••By Komal Nakrani

Fix: Astro Could Not Transform Image

Fix "Could not transform image" in Astro builds: find the bad file, re-export corrupt images, pull Git LFS files, and handle very large images with limitInputPixels.

#Astro#Errors#Images#Sharp

“Could not transform image.” means Astro’s image service (Sharp by default) failed while resizing or converting one of your images during the build. The message names the file: “Could not transform image /src/assets/hero.jpg. See the stack trace for more information.” Astro’s hint gives the most common cause: “This is often caused by a corrupted or malformed image. Re-exporting the image from your image editor may fix this issue.” Check that file first, then read the Sharp error in the stack trace.

Key Takeaways

  • The error names one file. Open it locally and confirm it’s a real, readable image.
  • Re-exporting from an image editor fixes most corrupt or oddly encoded files.
  • On CI, Git LFS pointer files that were never pulled look like images but aren’t.
  • Images over Sharp’s input pixel limit need a smaller source or a raised limitInputPixels.

Step 1: read the cause

The wrapper message is generic; the real reason is the Sharp error underneath it in the terminal. Typical ones:

Sharp says Meaning
Input buffer contains unsupported image format Not an image Sharp can read, or the extension lies
A libvips or decoder error naming the format Damaged file or unusual encoding
Input image exceeds pixel limit Very large source image

Step 2: check the file is really an image

# Terminal: what is this file, actually?
file src/assets/hero.jpg
ls -lh src/assets/hero.jpg

file should say “JPEG image data” or similar. If it says “ASCII text”, “HTML document” or the file is a few hundred bytes, it isn’t an image. Common reasons:

  • Git LFS pointer files. If images are stored with Git LFS and the build server didn’t pull them, each “image” is a small text file. Enable LFS in your host’s build settings or add git lfs pull to the build.
  • A failed download. An image saved from a URL that returned an error page.
  • A renamed file. A .png renamed to .jpg, or a HEIC photo from a phone renamed to .jpg. Convert it properly instead of renaming.

Step 3: re-export the image

Open the file in an image editor and export it again as JPEG, PNG or WebP. This fixes damaged files, unusual color profiles and odd encodings that some tools write. For a batch of files, Sharp’s own CLI or any image converter works; the goal is a clean, standard file.

Step 4: very large images

Sharp refuses images above its input pixel limit to protect memory. Resize huge originals before adding them to src/assets/; a 2,400 px wide source is plenty for most layouts. If you really need large sources, raise the limit for the Sharp service:

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

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

false removes the limit entirely, so only do it for images you trust, and watch build memory on CI.

Step 5: rebuild

Run npm run build again. If a different file fails next, repeat the check; a batch of images exported by the same tool often shares the problem. If Sharp itself is missing on the build server, you’ll get a different error, covered in could not find Sharp.

The Astro image component guide covers formats and sizes, and the image services guide explains when to swap Sharp for another service. Images in Komal Astro themes live in src/assets/ as described in our project structure docs. The official image service config lists all options.

FAQ

Why does the image work in the browser but not in Astro?

Browsers tolerate some damaged or unusual files that Sharp rejects, and they ignore a wrong extension. Check the real type with file and re-export the image.

Why does the build fail on CI but not locally?

Often Git LFS: locally you have the real images, while CI checked out only the pointer files. Make sure the build pulls LFS objects.

Can I skip one image instead of fixing it?

Move it to public/ and reference it with a plain <img>. Astro copies files in public/ without processing, so it won’t be optimized either.