New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

og-img

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

og-img

Generate dynamic Open Graph images for your website

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
95
decreased by-12.84%
Maintainers
1
Weekly downloads
 
Created
Source

og-img

This is a framework agnostic package for generating Open Graph images using Satori and resvg. Built using Web APIs, this package can be executed with Node.js and on the edge. You can use this package to add dynamic Open Graph images to your SvelteKit, Astro, SolidStart or Qwik website.

The difference to @vercel/og is that this package loads the WebAssembly module needed to convert SVG to PNG lazily at runtime and provides a framework agnostic workaround for defining the content of the image using satori-html.

Installation

This library is available for Node and Bun.

npm install og-img    # npm
yarn add og-img       # yarn
pnpm add og-img       # pnpm
bun add og-img        # bun

How it works

To generate an image, all you need to do is return an ImageResponse via a server endpoint (it probably will not work with SSG). You can use html to easily define the content of your image.

To get proper syntax highlighting for the tagged template literal in Visual Studio Code, you can install the lit-html extension.

import { fetchFont, ImageResponse, html } from 'og-img';

// With SvelteKit
export async function GET() {
  return new ImageResponse(
    // Use Tailwind CSS or style attribute
    html`
      <div tw="text-4xl text-green-700" style="background-color: tan">
        Hello, world!
      </div>
    `,
    {
      width: 1200,
      height: 600,
      fonts: [
        {
          name: 'Roboto',
          // Use `fs` (Node.js only) or `fetch` to read font file
          data: await fetchFont('https://www.example.com/fonts/roboto-400.ttf'),
          weight: 400,
          style: 'normal',
        },
      ],
    }
  );
}

// With Qwik
export const onGet = async ({ send }) => {
  send(
    new ImageResponse(
      // Use Tailwind CSS or style attribute
      html`
        <div tw="text-4xl text-green-700" style="background-color: tan">
          Hello, world!
        </div>
      `,
      {
        width: 1200,
        height: 600,
        fonts: [
          {
            name: 'Roboto',
            // Use `fs` (Node.js only) or `fetch` to read font file
            data: await fetchFont(
              'https://www.example.com/fonts/roboto-400.ttf'
            ),
            weight: 400,
            style: 'normal',
          },
        ],
      }
    )
  );
};

Then all you need to do is point to your API endpoint with a meta tag in the head of your website to embed the Open Graph image.

<head>
  <title>Hello, world!</title>
  <meta property="og:image" content="https://www.example.com/og-image" />
</head>

You can use URL parameters to dynamically change the content of your Open Graph image. Take a look at Valibot's Open Graph image. You can find the source code here.

Credits

Feedback

Find a bug or have an idea how to improve the library? Please fill out an issue. Together we can make the library even better!

License

This project is available free of charge and licensed under the MPL-2.0 license.

Keywords

FAQs

Package last updated on 25 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc