Giter VIP home page Giter VIP logo

ezog's Introduction

EZOG

EZOG is a JavaScript generator that can easily generate OG images without using a browser.

Features

  • Support for custom fonts.
  • Support for font size and line height.
  • Support for ellipsis.
  • Support for word breaking.
  • Support for specifying number of lines.
  • Support for Twemoji.
  • Support for embedding images.

Installation

npm install ezog

Example

import { generate, defaultFonts } from 'ezog';
import { fetch } from 'undici';

const png = await generate(
    [
        {
            type: 'image',
            buffer: baseImageBuffer, // Buffer / ArrayBuffer
            x: 0,
            y: 0,
            width: 1200,
            height: 630
        },
        {
            type: 'textBox',
            text: 'Hello, World',
            x: 0,
            y: 275,
            width: 1200,
            fontFamily: ['Sans', 'Noto Sans 700'],
            fontSize: 60,
            lineHeight: 80,
            lineClamp: 1, // optional
            align: 'center', // optional 'left' / 'right' / 'center'
            color: '#000' // optional
        }
    ],
    {
        width: 1200,
        height: 630,
        fonts: [
            {
                type: 'normalFont',
                name: 'Sans',
                data: fontSansBuffer // Buffer / ArrayBuffer
            },
            {
                type: 'googleFont',
                name: 'Noto Sans 700',
                googleFontName: 'Noto+Sans',
                weight: 700
            },
            ...defaultFonts(700 /* weight: optional */) // load multilingual Noto Sans from google fonts
        ],
        background: '#fff', // optional
        fetch: fetch // optional
    }
);

ezog's People

Contributors

kosei28 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ezog's Issues

Cloudflare Workers: The `initWasm()` function can be used only once.

Hello ๐Ÿ‘‹

Thanks for this amazing module. Unfortunately I receive the following error when using it inside of a Cloudflare Worker (only tested locally using the wrangler CLI).

โœ˜ [ERROR] Uncaught (in promise) Error: Already initialized. The `initWasm()` function can be used only once.

  throw new Error("Already initialized. The `initWasm()` function can be used only once.");
        ^
      at initWasm
  (/home/user/og-test/node_modules/@resvg/resvg-wasm/index.mjs:414:10)
      at generate
  (/home/user/og-test/node_modules/ezog/src/cloudflare/generate.ts:9:10)

On a cold start, this error doesn't appear (the png is generated just fine). However, when reloading the page (not the worker), this error appears. I'm not sure if this is behavior just in the local dev environment, or if this happens on Cloudflare as well.

The call to resvg-js's initWasm() in the generate() function seems to be the culprit (Ref). Patching the line fixes the issue. However, if there are any actual errors thrown by the resvg-js lib (ex. getImports() fails), then it could lead to catastrophic failures down the line.

--- index.js	2023-01-17 14:26:00.850899319 -0500
+++ index2.js	2023-01-17 14:29:45.079281456 -0500
@@ -295,7 +295,9 @@
 
 async function generate(elements, options) {
   const svg = await generateSvg(elements, options);
-  await initWasm(resvgWasm)
+  try {
+    await initWasm(resvgWasm)
+  } catch (_) {} // This "fixes" the issue but is bad in practice
   const resvg = new Resvg(svg, { fitTo: { mode: "original" } });
   const png = resvg.render().asPng();
   return png;

I tried looking more into resvg-js's documentation to see if there was some way to check if the wasm is initialized already, but I'm completely unfamiliar with the library.

Thanks in advance!

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.