astro-opengraph-image

(hopefully) the easiest way to generate OpenGraph images from your Astro site

Define your OpenGraph image with HTML/CSS, inline in your Astro components.

<figure>

the Astro toolbar with a preview of an OpenGraph image

<figcaption> Complete with a helpful toolbar app to display your image in development. </figcaption>

</figure>

Getting Started

# First, run this command to install the integration:
npx astro add astro-opengraph-image

# Next, you will want one or more fonts to use in your images,
# I like the fonts available at https://www.npmjs.com/org/fontsource, e.g.:
npm install @fontsource/inter
// Then, update your astro.config.{mjs|ts} file to configure the integration:
import { defineConfig } from "astro/config";
import opengraphImage from "astro-opengraph-image";
import { readFile } from "node:fs/promises";

export default defineConfig({
  integrations: [
    opengraphImage({
      // what color do you want your background to be?
      background: "#000000",

      // what size do you want your images to be?
      // 1200x630 is a good default across platforms,
      // and 3x scale is a convenient choice.
      width: 1200,
      height: 630,
      scale: 3,

      // the fonts you picked before. you will have to include the particular
      // weights and variants you want to use.
      fonts: [
        {
          name: "Inter",
          data: await readFile(
            "node_modules/@fontsource/inter/files/inter-latin-400-normal.woff",
          ),
          style: "normal",
          weight: 400,
        },
        {
          name: "Inter",
          data: await readFile(
            "node_modules/@fontsource/inter/files/inter-latin-700-normal.woff",
          ),
          style: "normal",
          weight: 700,
        },
      ],
    }),
  ],
});
---
// Lastly, inside your <head>, render the OgImage component to
// specify what you want in your image:

import { OgImage } from "astro-opengraph-image/components";
---

<!doctype html>
<html>
  <head>
    ...
    <OgImage>
      <h1>the page</h1>
      <p>this is the page</p>
      <style is:inline>
        h1 {
          color: red;
        }
      </style>
    </OgImage>
    ...
  </head>
  <body>...</body>
</html>

[!NOTE]

Your image is only influenced by code inside the <OgImage> tag. This means all relevant styles must live inside the tag.

Additionally, your styles must always have the is:inline attribute to convince Astro not to modify or hoist them.

[!NOTE]

astro-opengraph-image uses the Satori HTML layout engine and therefore supports the subset of HTML/CSS that Satori implements.

@sudovanilla/astro-opengraph-image - Installation

To install @sudovanilla/astro-opengraph-image via this registry, you'll need to install via the --registry parameter with your package manager.

n

NPM

npm install @sudovanilla/astro-opengraph-image --registry https://js.registry.sudovanilla.org

Learn more about the --registry parameter.

Yarn

yarn config set registry https://js.registry.sudovanilla.org
yarn install @sudovanilla/astro-opengraph-image

Learn more about the config option.

PNPM

pnpm install @sudovanilla/astro-opengraph-image --registry https://js.registry.sudovanilla.org

Learn more about the --registry parameter.

Bun Logo

Bun

bun install @sudovanilla/astro-opengraph-image --registry https://js.registry.sudovanilla.org

Learn more about the --registry parameter.

Learn more about setting this up in your bunfig.toml configuration.


For Deno, add the following to your .npmrc file in your directory:

registry="https://js.registry.sudovanilla.org"

Then run the deno install command.

@sudovanilla/astro-opengraph-image - Download

Download Tarball (v0.2.3)

Integrity sha512-y6W/X253M/N0QVItBJVYZ+pDP1OBFdXksquuED0YgSI8y3USocb7WijA/dsd9Wijv3PaeXt2sXUaTwrCgR2Mng==
SHASUM d6be389a1503dd86a0b969be843acd63e386595e
Tarball https://js.registry.sudovanilla.org/@sudovanilla/astro-opengraph-image/-/astro-opengraph-image-0.2.3.tgz