How to Automate Open Graph Images for Static Sites

Automate open graph images for static sites with a build script, 1200x630 PNGs, sRGB profiles, and automated meta tags.

How to Automate Open Graph Images for Static Sites

How to Automate Open Graph Images for Static Sites

Static site generators do not create social link previews automatically. When you share a page URL, platforms like Twitter, Facebook, and LinkedIn pull metadata from your HTML. If you omit the Open Graph <meta> tags, they fall back to a screenshot of your homepage or the first image they can find. Automating open graph images means your build pipeline generates a tailored preview card for every page. This gives consistent, branded link previews without manual work for each post.

TL;DR

You can automate open graph images by generating a PNG for each page during your build and injecting the corresponding <meta property="og:image"> tag into the page HTML. Use a fixed size of 1200x630 pixels with the sRGB color profile, and store the generated images in a public assets folder.

Prerequisites

You need a static site generator with a build hook. Common options include Next.js, Hugo, Jekyll, Astro, and Eleventy. Your generator must run a script during the build step so images are produced before the site is served.

You also need a programmatic image library. Examples include ImageMagick, Sharp for Node.js, Pillow for Python, and GD for PHP. These libraries let you draw text, shapes, and colors from code.

Finally, decide where your images live. The build output folder is the safest choice because it already contains the public files your web server serves.

Steps

1. Set up the generation script

Create a script that takes a page title, description, and optional background as input and outputs an image. Keep the output dimensions fixed. The recommended og:image size is 1200x630 pixels with an aspect ratio of 1.91:1. Social platforms crop and scale link preview images to roughly this ratio, so images far from it may be cropped awkwardly.

2. Draw the card content

Draw a background rectangle, then add the site name and page title as text. Use a legible font and keep enough padding around the edges. Leave room for a description line if your design includes one.

Convert the image to the sRGB color profile before saving. Web images should use the sRGB color profile so colors render consistently across browsers and devices. Some libraries save with an embedded profile by default, while others require an explicit conversion step.

3. Generate an image per page

Loop over your content entries and call the script for each one. Save each output with a unique filename, such as og-post-slug.png. This naming scheme makes it easy to reference the file from HTML.

4. Inject the meta tag

Add a hook in your generator that reads the generated filename and writes the following tag into the page head:

<meta property="og:image" content="/images/og-post-slug.png">

Some generators support a global layout or frontmatter field for this. Use whichever mechanism your tool provides, but make sure the path points to the public build folder.

5. Verify the output

Run the build and open a few generated pages in a browser. Inspect the page source and confirm the og:image tag exists and points to a real file. Then use a social platform preview tool, such as the Facebook Sharing Debugger or the Twitter Card Validator, to check how the card renders.

Comparison table

ApproachEffortConsistencyCustomization
Hand-crafted static imagesHigh per pageInconsistentFull control
Platform default previewNoneInconsistentNone
Automated generation in buildOne-time setupConsistentHigh

Automated generation in the build step offers the best balance of consistency and customization.

Expected Result

After the build finishes, each page has its own preview image and a matching meta tag. Link previews show the correct title, colors, and layout across all platforms. Readers see a branded card instead of a generic screenshot.

Troubleshooting

  • The image does not appear in the preview tool. Check that the URL in og:image is absolute or starts with / and resolves from the site root.
  • Colors look washed out or shifted. Confirm the image is in the sRGB color profile.
  • The title gets cut off. Reduce the font size or shorten the title text.
  • The build takes much longer. Only regenerate images for pages that changed, or cache previously generated output.

Trade-offs

Automating open graph images adds a build step and increases build time. You also need to maintain the generation script when your design changes. However, the alternative is manual work for every new page, which does not scale.

Alt text for images improves accessibility for screen-reader users and helps search engines understand image content. While Open Graph tags do not carry alt text, the same accessibility mindset applies when you design the card. Make sure text is large enough and has sufficient contrast so it reads clearly at small sizes.

FAQ

How do I generate Open Graph images automatically? Run an image library in your build step, save a 1200x630 pixel PNG for each page, and add an og:image meta tag that points to it.

What size should an Open Graph image be? Use 1200x630 pixels, which matches the recommended og:image size and the 1.91:1 aspect ratio that most platforms expect.

Why do my Open Graph images look different on different platforms? Each platform crops and scales to roughly the same ratio, but their preview boxes vary. Keep the most important content near the center and avoid placing text near the edges.

Do I need a separate image for every page? No, but it helps. A shared default image works for unlinked pages, while page-specific images give better context on social feeds.

Can I use a template instead of generating images by hand? Yes. A template lets your script draw text over a reusable background, which is how automation stays consistent across hundreds of pages.

PicForge offers a free OG Image Generator that creates social preview cards without signup. Use it to design a template, then apply the same settings in your automated build pipeline.