Skip to content

About Open Graph Images

· 3 min

This post explains how to configure Open Graph (OG) images in the Astro AntfuStyle Theme.

What Are Open Graph Images?#

OG images, short for Open Graph images, are metadata images used by social media platforms to visually represent a webpage when shared. Originating from Facebook’s Open Graph protocol, they make shared content more engaging.

Platforms like Facebook, Twitter, LinkedIn, and Discord use these images. While Twitter uses its own protocol (Twitter Cards), “OG image” is often used as a general term for these images.

Configuring OG Images#

There are three ways to configure OG images in this theme, listed by priority:

Method 1: Specify the ogImage field in the Markdown/MDX frontmatter with a custom image, which should be saved in /public/og-images/. For example:

src/content/blog/about-open-graph-images.md
---
title: About Open Graph Images
# The specified OG image must be saved: `public/og-images/specified-og-image.png`
ogImage: specified-og-image.png
---
If the image isn’t found in /public/og-images/, a fallback og-image.png will be used.

Method 2: Set ogImage to true or remove the field to automatically generate an OG image, saved in /public/og-images/. The generated filename will match the Markdown/MDX file, or the directory if it’s named index.md or index.mdx. For example:

src/pages/blog/index.mdx
---
title: Blog
# Auto-generated OG image is saved: `public/og-images/blog.png`
ogImage: true
---
OG images won’t generate if the frontmatter has redirect, draft: true, or is missing the title field.

Method 3: Set ogImage to false to prevent generation and use the fallback OG image instead. A fallback OG image named og-image.png is stored in /public/og-images/, ensuring that any page shared on social platforms displays an image.

If you need to regenerate the fallback OG image or after modifying FEATURE.ogImage, delete og-image.png to create a new fallback image.

Template for Auto-Generated OG Images#

The template (found in plugins/og-template/markup.ts) for auto-generated OG images is built with HTML + UnoCSS and processed by ==natemoo-re/satori-html==. It takes three parameters:

For details on how to replace the logo in the template, refer to ==Advanced Configuration - Customizing Logo==.

Here are examples of OG images generated for the four bgType options: ‘plum’, ‘dot’, ‘rose’, ‘particle’:

==(补图)==

How This Theme Automatically Generates OG Images#

This theme uses a custom remark plugin (found in plugins/remark-generate-og-image.ts) to handle the automatic generation of OG images. The decision to use a remark plugin instead of creating Astro endpoints was made for the following reasons:

Extra Tips#

Auto-generated OG images are compressed with lovell/sharp. For custom images, consider ==manual compression==.

If an image with the same name as the Markdown/MDX file (or the directory for index.md/index.mdx) already exists in /public/og-images/, it won’t be regenerated. To regenerate it, manually delete the corresponding image from the /public/og-images/ directory.

If you want to globally disable the auto-generation, set FEATURES.ogImage to false or [false, {...}] in src/config.ts.

Thank you for reading. If you encounter issues, find errors, or see opportunities for improvement, feel free to join the discussion or submit an issue or pull request. Your feedback is highly appreciated!

> share on twitter / mastodon
> cd ..