Fields

Image

An image from the media library, with responsive sizes.

An image field holds an image from the media library. For PDFs and other files, use a file field.

The image field in the admin

Example

cms/collections/posts.ts
export const posts = defineCollection({
  fields: {
    hero: { type: "image", required: true },
    gallery: { type: "image", many: true },
    avatar: { type: "image", maxSize: 2 * 1024 ** 2, folder: "avatars" },
  },
});

A read loads the image, like a relation:

{
  id: "m1",
  url: "/_tenon/media/files/blog/hero.jpg",
  src: "/_tenon/media/img/w_1920&f_webp&q_85/blog/hero.jpg",
  srcset: "/_tenon/media/img/w_320&f_webp&q_85/blog/hero.jpg 320w, …",
  width: 2400,
  height: 1600,
  alt: "Harbour at dusk",
  mimeType: "image/jpeg",
  size: 482133,
  folder: "blog",
  filename: "hero.jpg",
  key: "blog/hero.jpg",
}
  • url is the original. src and srcset are the resized copies.
  • With populate: false, the field holds the id.

Render it

The Image component writes src, srcset, width, height and alt:

---
import { Image } from "tenoncms/astro/components";
---

<Image image={post.hero} sizes="(min-width: 720px) 720px, 100vw" />

{post.gallery?.map((image) => <Image image={image} sizes="33vw" />)}

sizes tells the browser how wide the image shows, so it picks the right copy from srcset. See Components for Picture.

With or without an image provider

An image provider makes the resized copies. You do not have to use one.

SetupsrcsrcsetUse it when
No providerThe originalEmptyThe images are small, or the framework resizes them for you
With a providerA resized WebP copyOne per widthYou want responsive images without another tool
  • Without a provider, the Image component still renders width, height and alt. The browser loads the original.
  • With a provider, set media.images in the media config. Tenon supports ipx, cloudflare, vercel, netlify, imgix and more.
  • To pick one, see Do you need one? and Providers.

Image sizes

With an image provider, image sets how a read builds src and srcset. Use a named preset, or set the options on the field:

avatar: { type: "image", image: { preset: "avatar" } },
cover: { type: "image", image: { sizes: [800, 1600], quality: 70 } },
banner: { type: "image", image: { width: 1600, height: 600, fit: "cover" } },

The field's own options override the preset's. Sizes and presets are set with the provider. See Sizes and presets.

Use your framework's image component

Instead of a provider, you can let the framework resize the image. Leave media.images off and pass url, width, height and alt to the framework's own component.

url is a path on your site. The framework's optimizer MUST get the full URL, and it MUST allow your site's host.

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

const hero = post.hero;
const src = new URL(hero.url, Astro.site).href;
---

<Image src={src} width={hero.width} height={hero.height} alt={hero.alt ?? ""} />

Allow the host in the framework config:

export default defineConfig({
  site: "https://example.com",
  image: { domains: ["example.com"] },
});
  • Astro needs site set, so it can build the full URL. Without width and height, add inferSize.
  • Nuxt needs the @nuxt/image module.
  • The field preset and image options do not apply, because Tenon makes no copies.

Deleting an image

An image that a field uses cannot be deleted. The delete fails with a 409 and lists the documents that use it. Set onDelete: "set-null" to clear the field instead.

Options

Store a list of images instead of one. With required, at least one. Default false.The allowed image types, like ["image/png", "image/webp"]. Default: all images.The largest file in bytes. It narrows media.maxSize.The folder that Upload new in the field puts the image in, like "avatars". Default: the root.What happens when the image is deleted. Default "restrict". A required field cannot use "set-null".How reads build src and srcset. Needs media.images. See Image sizes.