GithubHelp home page GithubHelp logo

og-generator's Introduction

Railway OG Image Generator

Deploy on Railway

Service that dynamically generates Open Graph images for Railway starters that looks something like

โœจ How To Use

Use the generated image URL in the <head> of your HTML document as the og:image meta property

  <meta property="og:image" content="https://og.railway.app/api/image?fileType=png&layoutName=Simple&Text=**Hello**+_World_" />

Whenever this image is requested (e.g. in link previews) the image will be generated on demand.

๐Ÿง How It Works

Images are generated through the /api/image route. When you hit this route the following happens

  • Query params are parsed
  • Layout is looked up in list of layouts using the layoutName query param
  • layout.getCSS called with all query params
  • layout.Component is rendered with all query params as config prop
  • HTML page built, rendered with Puppeteer, and screenshot
  • Screenshot returned with a long cache max age

Layouts

This service can generate images using multiple layouts. A layout is defined as a

  • Collection of properties that are user configurable. The UI for these properties is auto genearted
  • Function that takes in layout config and returns CSS needed to render
  • A React component that takes in layout config as a prop

For example, the "Simple" layout on og.railway.app is defined as

import { GetCSSFn, ILayout, LayoutComponent } from "../types";
import { gString, Markdown } from "./utils";

const getCSS: GetCSSFn = config => {
  return `
    body {
      font-size: 200px;
      color: white;
      background: linear-gradient(to bottom right, tomato, deeppink);
    }
  `;
};

const Component: LayoutComponent = ({ config }) => {
  const text = gString(config, "Text");
  return <h1>{text}</h1>;
};

export const simpleLayout: ILayout = {
  name: "Simple",
  properties: [{ name: "Text", type: "text", default: "**Hello** _World_" }],
  getCSS,
  Component,
};

This will render as

image

๐Ÿš€ Development

You can fork this repo or deploy to Railway to make it your own, customize, and use in your own projects.

The frontend is a NextJS site and the image generation happens in an API route.

# Start local development server
yarn dev

# Build for production
yarn build

# Start in production
yarn start

๐Ÿ™Œ Acknowledgement

Credit where credit is due. This started as a forked repo from Vercel's OG image generator. The main differences are

  • Multiple configurable layouts
  • Content of image written in JSX (as opposed to a template string)
  • Headless Chrome configuration modified to deploy on Railway

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.