Coder Social home page Coder Social logo

maik-wi / ogimage-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from itbruno/ogimage-generator

0.0 0.0 0.0 433 KB

Open-graph image generator with @vercel/og

Home Page: https://ogimage-generator-sigma.vercel.app

JavaScript 2.26% TypeScript 97.55% HTML 0.19%

ogimage-generator's Introduction

OGImage generator

This is a open-source project to generate dynamic Open-graph images using @vercel/og package.

The purpose is generate custom open-graph images passing an custom themes as param

  • ๐Ÿ”— Demo 1 with default theme

    • Ex.: /api/ogimage?title=something&subtitle=Anything
  • ๐Ÿ”— Demo 2 with custom theme as param ( &theme=blog)

    • Ex.: /api/ogimage?title=something&subtitle=Anything&theme=blog

Requirements

  • Node.js >16

Creating a custom theme

The all themes are created on ./src/og-themes folder. To create a new theme and keep available to use on api, follow these steps:

Creating a new theme as React component

Go to ./src/og-themes and create a new file. Ex.: EventTheme.tsx

// ./src/og-themes/EventTheme.tsx

interface EventThemeProps {
  params: {
    title: string,
    subtitle: string
  };
}

function EventTheme({ params }: EventThemeProps) {
  return (
    <div
      style={{
        width: 1200,
        height: 630,
        alignItems: 'center',
        justifyContent: 'center',
        display: 'flex',
        backgroundColor: '#ffcc00',
        fontSize: 40
      }}
    >
      {params.title}
      <br />
      {params.subtitle}
    </div>
  );
}

export { EventTheme };

After new theme already created, export it on ./src/og-themes/index.tsx

export { EventTheme } from '@themes/EventTheme';

And now you already to add the new theme to be available to use on api

Add a new case on ./src/pages/api/ogimage.tsx

// ./src/pages/api/ogimage.tsx
import { EventTheme } form '@themes/index';

...

switch (PARAMS.theme) {
    case 'event':
      selectedTheme = <EventTheme params={PARAMS} />;
      break;
    default:
      selectedTheme = <DefaultTheme params={PARAMS} />;
      break;
  }

Now you can run and test your new theme at localhost

Get started

This project is running with NextJS, to run locally, install all dependencies I use yarn to run all commands, but you can use npm if you want

Install dependencies

$ yarn install

Run localhost

$ yarn dev

Sample

http://localhost:3000/api/ogimage?title=My awesome title&subtitle=Front-end Developer&theme=event

ogimage-generator's People

Contributors

itbruno avatar

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.