DEV Community

Cover image for ๐Ÿ–ผ๏ธ Building a Dynamic OG Image Generator with Cloudflare Workers: Edge-Powered Social Cards Made Easy
Mohammed Abdulatef
Mohammed Abdulatef

Posted on

๐Ÿ–ผ๏ธ Building a Dynamic OG Image Generator with Cloudflare Workers: Edge-Powered Social Cards Made Easy

๐Ÿ‘‹ Hey DEV community!

Iโ€™m excited to share Dynamic OG Image Generator, an open-source project I built to simplify generating dynamic Open Graph images for blogs, apps, and social media. It leverages Cloudflare Workersโ€™ edge network for blazing-fast performance, and Iโ€™d love your feedback (and a star โญ)!


The Problem

Social platforms like Twitter and LinkedIn rely on Open Graph images for rich link previews. But:

  • Static images get stale quickly
  • Dynamic solutions often require complex backends or paid services
  • Font/licensing issues can derail designs

This project solves those pain points with edge computing and flexible customization.


Key Features

โœ… Edge-First Performance

Runs on Cloudflare Workersโ€™ global network, generating images in <100ms.

โœ… 4 Visual Styles

Pre-built templates for different use cases:

  • Modern gradients
  • Eco/tech themes
  • Branded designs (e.g., Cloudflare)
  • GitHub-style profile cards

โœ… Font Loading Flexibility

Choose your strategy without licensing headaches:

// Example: Load Google Fonts  
const font = await googleFont(text, 'Noto Sans JP', 900, 'normal');  

// Or self-host fonts via GitHub  
const font = await githubFonts();  
Enter fullscreen mode Exit fullscreen mode

โœ… Automatic Caching

Reduce compute costs by caching fonts and generated images:

// Enable cache middleware  
og.get('/og', ...middleware.cache());  
Enter fullscreen mode Exit fullscreen mode

โœ… Tailwind CSS Integration

Style cards with familiar utility classes for rapid iteration.


How It Works

  1. Send a GET request with parameters:
   /og?mainText=Hello World&description=Edge-Powered OG Magic&style=1  
Enter fullscreen mode Exit fullscreen mode
  1. Get a 1200x630px social-ready image optimized for Twitter, LinkedIn, etc.

Sample Response:

OG Image Example


Quickstart Guide

  1. Clone the repo:
   git clone https://github.com/mohdlatif/og-image-generator-cloudflare-worker  
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies (uses Bun ๐Ÿš€):
   bun install  
Enter fullscreen mode Exit fullscreen mode
  1. Deploy to Cloudflare:
   bun run deploy  
Enter fullscreen mode Exit fullscreen mode

For a detailed walkthrough, check out my Cloudflare Workers tutorial.


Why This Matters

  • Cost-effective: No need for dedicated image servers
  • Scalable: Workers handle spikes effortlessly
  • Customizable: Add your own styles/fonts in minutes

๐Ÿ”— GitHub Repo

mohdlatif/og-image-generator-cloudflare-worker

If you find this useful:

  1. โญ Star the repo to help others discover it
  2. ๐Ÿ’ฌ Share feedback โ€“ Iโ€™d love to hear your use cases!
  3. ๐Ÿ› ๏ธ Contribute: PRs welcome for new styles, optimizations, or docs!

Letโ€™s make dynamic OG images a solved problem for everyone! ๐Ÿš€

P.S. Big thanks to the Hono and Vercel OG teams for their amazing tools!

Top comments (0)