Images
How to generate OG images automatically for every page
Three ways to give every page its own 1200x630 card image without designing each one: build-time scripts, framework image routes, or an on-demand service.
Short answer
Generate OG images automatically by rendering a template with each page's title into a 1200x630 PNG, either in a build script, through your framework's image route such as Next.js opengraph-image, or with an on-demand service, then point og:image at the result.
Key takeaways
- A template plus the page title gives every page a distinct card with no manual design.
- Build-time generation is the simplest and cheapest: static files, cached forever.
- Next.js has an opengraph-image file convention with ImageResponse from next/og.
- Version the image URL when the title changes, or X will keep the old picture.
A site with forty articles and one shared OG image looks the same in every link preview: same logo, same tagline, whatever the article is about. Designing forty images by hand isn't realistic, and nobody keeps it up. The answer is a template. Each page passes its title, maybe a category and an icon, and a script turns that into a 1200 x 630 image.
How does automatic OG image generation work?
Automatic generation renders one design template with each page's own text and saves the result as an image that the page's og:image tag points to.
The pieces are always the same:
- A template. Background, brand mark, a title area and optional extras like a category label.
- Page data. Title, and sometimes a subtitle, author, date or icon.
- A renderer. Something that draws text onto an image: an image library, a headless browser or an SVG-to-PNG engine.
- A URL. The generated file is saved or served at a stable address, and the page's
og:imageandtwitter:imagetags point to it.
Where the rendering happens is the main choice.
Which approach should I use?
Generate at build time for static sites, use your framework's image route if it has one, and reach for an on-demand service only when pages are created faster than you build.
| Approach | Where it runs | Best for | Watch out for |
|---|---|---|---|
| Build-time script | Your build machine | Blogs, docs, marketing sites | Build time grows with page count |
| Framework image route | Framework, at build or on request | Next.js, and similar frameworks with an equivalent | Fonts must be loaded explicitly |
| On-demand service | A function or API at request time | User-generated pages, huge catalogs | Cold starts, caching, cost |
Build-time script
A script runs during your build, loops over pages and writes og/<slug>.png for each. Python with Pillow, Node with an image library or a headless browser screenshotting an HTML template all work. Cache by a hash of the title so unchanged pages aren't redrawn. The output is plain static files, which any host can serve and every crawler can fetch.
Next.js image routes
Next.js has a file convention for this. Add opengraph-image.tsx to a route segment, return an ImageResponse from next/og, and export size and alt. Next.js writes the og:image, width, height and alt tags for that route. For dynamic routes such as app/posts/[slug]/, the function receives the route params, so each post renders its own title. According to the Next.js docs, these images are generated at build time and cached by default unless they depend on request-time data.
The renderer underneath, Satori, supports a subset of CSS based on flexbox. Plan the layout with flex containers, and load your font file explicitly, or the text falls back to a default.
On-demand services
A function that takes a title in the query string and returns a PNG is flexible, and it's the only practical option when users create pages faster than you can build. Put a CDN cache in front of it. Validate the input so nobody can make your domain render arbitrary text, and keep the query string short and predictable.
What makes a generated card readable?
A readable card has a large title that wraps cleanly, high contrast and nothing important near the edges.
- Size the title for a phone. The preview may be shown around 500 pixels wide. A title smaller than about 48 pixels on the 1200-pixel canvas gets hard to read.
- Handle long titles. Shrink the font step by step until the title fits in three or four lines, and cut with an ellipsis only as a last resort.
- Keep a safe margin. X shows large-image cards close to 2:1, so a 1200 x 630 image can lose a sliver at the top and bottom. Keep text at least 40 pixels from the edges.
- High contrast. Dark text on light or the reverse. Avoid busy photos behind the title.
- Brand once. A small logo or domain in a corner is enough.
The OG Image Maker is handy for designing the template. Try a long title and a short one, check how they wrap, then copy the layout into your script. Our image sizes cheat sheet has the numbers for each platform.
How do I keep generated images fresh?
Change the image URL whenever the image changes, because X and other platforms cache images by their URL.
If you regenerate og/launch.png with a new title and keep the same file name, X may go on showing the old picture. Put a short hash of the title in the file name, such as og/launch-3f9a1c.png, or add a version query string. The guide on how X caches cards explains why.
Do generated images need alt text?
Yes, set twitter:image:alt and og:image:alt for every generated image, usually to the page title or a short description.
Generated images usually contain the page title as text. Set twitter:image:alt and og:image:alt to that title or a short description, so people using screen readers get the same information. In Next.js, the exported alt constant does this. Our guide to alt text for card images has examples.
How do I test the template before launch?
Test the template with a very short title, a very long title and a title full of unusual characters.
Before you ship, pick three pages: one with a very short title, one with a very long title and one with unusual characters such as quotes, accents or an ampersand. Open their generated images directly in the browser. If all three look right, the template will hold up for the rest.
Questions
What size should a generated OG image be?
1200 x 630 pixels works across X, Facebook, LinkedIn and most messaging apps. X shows large image cards close to 2:1, so keep the important text away from the top and bottom edges.
PNG or JPG for generated cards?
PNG for cards that are mostly text and flat color, because the letters stay sharp. Use JPG if the template includes a photo and the PNG gets large.
Do generated images slow down my site?
Not for visitors, because OG images are fetched by the platforms that build previews, not by people loading your page. Build-time generation can lengthen your build, so cache images and only regenerate when the title changes.
Should every page have its own image?
Pages people share do: articles, products, tools and docs. Legal pages and search results can use a default card.