To use an image in Astro frontmatter, write a relative path like cover: ./cover.jpg, and in src/content.config.ts define the field with the image() helper: schema: ({ image }) => z.object({ cover: image() }). Astro checks the file exists at build time and turns the path into image metadata you pass straight to <Image />.
Key Takeaways
image()comes from the schema function’s context, not from an import.- Paths are relative to the Markdown file, so images can live next to posts.
- The result is
ImageMetadata(src,width,height,format), ready for<Image />.- Chain
.refine()to enforce minimum sizes, and store alt text in its own field.
Define the schema
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: ({ image }) =>
z.object({
title: z.string(),
pubDate: z.coerce.date(),
cover: image().refine((img) => img.width >= 1200, {
message: 'Cover images must be at least 1200px wide',
}),
coverAlt: z.string(),
}),
});
export const collections = { blog };
The schema is a function here, which is how you receive image. .refine() runs on the processed metadata, so you can check width, height or format. A wrong path fails the build with an image-not-found error instead of shipping a broken page; the image not found fix covers that error.
Write the frontmatter
---
title: "Deploying Astro to Cloudflare Pages"
pubDate: 2026-10-20
cover: ./cloudflare-cover.jpg
coverAlt: "Terminal showing a successful wrangler deploy"
---
Put the image next to the post (src/content/blog/cloudflare-cover.jpg) or in a shared folder (../../assets/covers/cloudflare.jpg). Paths must start with ./ or ../. Images in public/ aren’t processed and can’t be validated by image(); use a plain z.string() for those.
Render it
---
// src/pages/blog/[id].astro (excerpt)
import { Image } from 'astro:assets';
const { post } = Astro.props;
const { cover, coverAlt, title } = post.data;
---
<article>
<Image src={cover} alt={coverAlt} widths={[640, 960, 1200]} sizes="(min-width: 960px) 960px, 100vw" />
<h1>{title}</h1>
</article>
cover is ImageMetadata, so <Image /> knows the original dimensions, sets width and height to prevent layout shift, and outputs optimized WebP. widths and sizes generate a responsive srcset. On a listing page, pass a smaller width for thumbnails; each size is generated once at build time. Our image component guide covers every option.
Use it for Open Graph images
Social cards need an absolute URL to a real file. Use getImage() to produce an optimized JPEG and build the URL from site:
---
// src/components/SEO.astro (excerpt)
import { getImage } from 'astro:assets';
import type { ImageMetadata } from 'astro';
interface Props { image?: ImageMetadata }
const { image } = Astro.props;
const og = image ? await getImage({ src: image, width: 1200, height: 630, format: 'jpeg' }) : null;
---
{og && <meta property="og:image" content={new URL(og.src, Astro.site).href} />}
JPEG is the safest format for social platforms. Set site in astro.config.mjs so the URL is absolute.
Optional images and remote URLs
- Optional cover:
cover: image().optional(), then guard with{cover && <Image ... />}. - Remote images:
image()only accepts local files. For a URL, usez.url()and allow the domain inimage.domainsso<Image />can optimize it, passingwidthandheightorinferSize. - Images inside the body: relative images in Markdown (
) are optimized automatically;image()is only for frontmatter.
The Content Collections guide and docs page cover schemas more broadly. Many blog themes already define a cover field this way; check content.config.ts before adding another.
FAQ
How do I add an image to frontmatter in Astro?
Write a relative path to the image in frontmatter, such as cover: ./cover.jpg, and declare the field with image() from the schema context: schema: ({ image }) => z.object({ cover: image() }). Then pass post.data.cover to <Image />.
Why does my frontmatter image path not work?
Paths are resolved relative to the Markdown file and must start with ./ or ../. Paths starting with / point to public/, which image() can’t process. Check the path from the post’s folder and the file name’s case.
Can I use image() with remote URLs?
No. image() validates and imports local files. For remote images, store the URL as a string with z.url(), add the host to image.domains, and give <Image /> dimensions or inferSize.