|
1 | 1 | /** |
2 | 2 | * OG Image Generation API |
3 | 3 | * |
4 | | - * For now, returns a simple redirect to static images or generates |
5 | | - * a basic HTML response. Full image generation requires additional |
6 | | - * setup with image processing libraries. |
7 | | - * |
8 | | - * TODO: Implement full image generation using: |
9 | | - * - @vercel/og (recommended for Vercel deployment) |
10 | | - * - OR canvas (for self-hosted) |
11 | | - * - OR satori + resvg (for static exports) |
| 4 | + * Generates Open Graph images dynamically using SVG. |
| 5 | + * Returns SVG images that can be displayed directly by browsers |
| 6 | + * and social media crawlers. |
12 | 7 | * |
13 | 8 | * Usage: /api/og?icon=tabler/IconRun&title=Post Title |
14 | 9 | */ |
15 | 10 |
|
16 | | -import { getIcon } from '@pog/lib/iconMapper' |
| 11 | +import { createOGImageSVG } from '@pog/lib/iconRenderer' |
17 | 12 |
|
18 | 13 | export default async function handler(req, res) { |
19 | 14 | try { |
20 | 15 | const { |
21 | 16 | icon = 'tabler/IconBooks', |
22 | | - title = 'Livro POG' |
| 17 | + title = '' |
23 | 18 | } = req.query |
24 | 19 |
|
25 | | - // Verificar se o ícone existe |
26 | | - const IconComponent = getIcon(icon) |
| 20 | + // Generate SVG |
| 21 | + const svg = createOGImageSVG(icon, title) |
27 | 22 |
|
28 | | - if (!IconComponent) { |
29 | | - return res.status(404).json({ |
30 | | - error: 'Icon not found', |
31 | | - icon |
32 | | - }) |
33 | | - } |
34 | | - |
35 | | - // Por enquanto, retorna JSON com informações |
36 | | - // Em produção, isso geraria uma imagem PNG/JPEG |
37 | | - res.status(200).json({ |
38 | | - message: 'OG Image generation endpoint', |
39 | | - icon, |
40 | | - title, |
41 | | - width: 1200, |
42 | | - height: 630, |
43 | | - note: 'Full image generation requires @vercel/og or canvas setup' |
44 | | - }) |
| 23 | + // Set headers for SVG response |
| 24 | + res.setHeader('Content-Type', 'image/svg+xml') |
| 25 | + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') |
| 26 | + |
| 27 | + // Send SVG |
| 28 | + res.status(200).send(svg) |
45 | 29 |
|
46 | 30 | } catch (error) { |
47 | | - console.error('Error in OG image endpoint:', error) |
48 | | - res.status(500).json({ |
49 | | - error: 'Internal server error', |
50 | | - message: error.message |
51 | | - }) |
| 31 | + console.error('Error generating OG image:', error) |
| 32 | + |
| 33 | + // Return error SVG |
| 34 | + const errorSVG = ` |
| 35 | +<svg width="1200" height="630" xmlns="http://www.w3.org/2000/svg"> |
| 36 | + <rect width="1200" height="630" fill="#1a1a2e"/> |
| 37 | + <text x="600" y="315" font-family="system-ui" font-size="40" fill="#ff6b6b" text-anchor="middle"> |
| 38 | + Error generating image |
| 39 | + </text> |
| 40 | + <text x="600" y="360" font-family="system-ui" font-size="20" fill="#999" text-anchor="middle"> |
| 41 | + ${error.message} |
| 42 | + </text> |
| 43 | +</svg>` |
| 44 | + |
| 45 | + res.setHeader('Content-Type', 'image/svg+xml') |
| 46 | + res.status(500).send(errorSVG) |
52 | 47 | } |
53 | 48 | } |
54 | 49 |
|
| 50 | + |
0 commit comments