|
| 1 | +import { ImageResponse } from "next/og"; |
| 2 | + |
| 3 | +async function loadGoogleFont(font: string, text: string) { |
| 4 | + const url = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}`; |
| 5 | + const css = await (await fetch(url)).text(); |
| 6 | + const resource = css.match( |
| 7 | + /src: url\((.+)\) format\('(opentype|truetype)'\)/, |
| 8 | + ); |
| 9 | + |
| 10 | + if (resource) { |
| 11 | + const response = await fetch(resource[1]); |
| 12 | + if (response.status == 200) { |
| 13 | + return await response.arrayBuffer(); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + throw new Error('failed to load font data'); |
| 18 | +} |
| 19 | + |
| 20 | +export async function GET(request: Request) { |
| 21 | + const { searchParams } = new URL(request.url); |
| 22 | + const title = searchParams.get("title"); |
| 23 | + const description = searchParams.get("description"); |
| 24 | + |
| 25 | + |
| 26 | + return new ImageResponse( |
| 27 | + ( |
| 28 | + <div tw="flex h-full w-full bg-white text-black" style={{ fontFamily: "Work Sans" }}> |
| 29 | + <div tw="border absolute border-neutral-200 border-dashed inset-y-0 left-16 w-[1px]" /> |
| 30 | + <div tw="border absolute border-neutral-200 border-dashed inset-y-0 right-16 w-[1px]" /> |
| 31 | + <div tw="border absolute border-neutral-200 inset-x-0 h-[1px] bottom-16" /> |
| 32 | + <p tw="absolute bottom-2 right-24 text-neutral-600"> |
| 33 | + @paceui |
| 34 | + </p> |
| 35 | + <div tw="flex flex-col absolute w-[896px] inset-x-32 inset-y-24"> |
| 36 | + <img src="https://paceui.com/images/brand/logo-light.svg" tw="h-20" alt="Logo"/> |
| 37 | + <div tw="flex flex-col mt-24"> |
| 38 | + <div |
| 39 | + tw="tracking-tight flex-grow-1 flex flex-col justify-center leading-[1.1]" |
| 40 | + style={{ |
| 41 | + textWrap: "balance", |
| 42 | + fontSize: title && title.length > 20 ? 64 : 80, |
| 43 | + letterSpacing: "-0.04em", |
| 44 | + }}> |
| 45 | + {title} |
| 46 | + </div> |
| 47 | + <div |
| 48 | + tw="text-[40px] leading-[1.5] flex-grow-1 text-neutral-600 mt-4" |
| 49 | + style={{ |
| 50 | + textWrap: "balance", |
| 51 | + }}> |
| 52 | + {description} |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ), |
| 58 | + { |
| 59 | + width: 1200, |
| 60 | + height: 630, |
| 61 | + fonts: [ |
| 62 | + { |
| 63 | + name: 'Work Sans', |
| 64 | + data: await loadGoogleFont( |
| 65 | + 'Work Sans', |
| 66 | + `${title} ${description} @paceui`, |
| 67 | + ), |
| 68 | + style: 'normal', |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + ); |
| 73 | +} |
0 commit comments