-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathopengraph-image.tsx
More file actions
154 lines (143 loc) · 3.62 KB
/
opengraph-image.tsx
File metadata and controls
154 lines (143 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { ImageResponse } from "next/og";
export const runtime = "edge";
export const alt = "Blog - Magic UI";
export const size = {
width: 1200,
height: 630,
};
export const contentType = "image/png";
const getAssetData = async () => {
try {
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL;
const fontUrls = {
clashDisplay: `${baseUrl}/fonts/ClashDisplay-Semibold.ttf`,
cabinetGrotesk: `${baseUrl}/fonts/CabinetGrotesk-Medium.ttf`,
logo: `${baseUrl}/magicui-logo.png`,
};
const [clashDisplayRes, cabinetGroteskRes, logoRes] = await Promise.all([
fetch(fontUrls.clashDisplay),
fetch(fontUrls.cabinetGrotesk),
fetch(fontUrls.logo),
]);
if (!clashDisplayRes.ok || !cabinetGroteskRes.ok || !logoRes.ok) {
return null;
}
const [clashDisplay, cabinetGrotesk, logoImage] = await Promise.all([
clashDisplayRes.arrayBuffer(),
cabinetGroteskRes.arrayBuffer(),
logoRes.arrayBuffer(),
]);
const logoBase64 = `data:image/png;base64,${Buffer.from(logoImage).toString(
"base64"
)}`;
return {
clashDisplay,
cabinetGrotesk,
logoBase64,
};
} catch (error) {
console.error("Failed to load assets:", error);
return null;
}
};
const styles = {
wrapper: {
height: "100%",
width: "100%",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
backgroundColor: "white",
padding: "40px",
},
container: {
height: "100%",
width: "100%",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
border: "4px solid black",
padding: "60px",
},
title: {
fontSize: "64px",
color: "black",
marginBottom: "10px",
textAlign: "center",
fontFamily: "Clash Display",
letterSpacing: "0.5px",
},
description: {
fontSize: "28px",
color: "black",
textAlign: "center",
maxWidth: "800px",
fontFamily: "Clash Display",
letterSpacing: "0.5px",
border: "3px solid black",
padding: "10px 15px",
borderRadius: "100px",
},
} as const;
export default async function Image() {
try {
const assetData = await getAssetData();
return new ImageResponse(
(
<div
style={{
...styles.wrapper,
fontFamily: assetData ? "Clash Display" : "system-ui",
}}
>
<div style={styles.container}>
<img
src={
assetData?.logoBase64 ||
`${process.env.NEXT_PUBLIC_SITE_URL}/magicui-logo.png`
}
alt="MagicUI Logo"
width={100}
height={100}
/>
<h1 style={styles.title}>Blog</h1>
<p style={styles.description}>
A blog about design, development, and other things.
</p>
</div>
</div>
),
{
...size,
fonts: assetData
? [
{
name: "Clash Display",
data: assetData.clashDisplay,
weight: 500,
style: "normal",
},
{
name: "Cabinet Grotesk",
data: assetData.cabinetGrotesk,
weight: 500,
style: "normal",
},
]
: undefined,
}
);
} catch (error) {
console.error("Error generating image:", error);
return new Response(
`Failed to generate image: ${
error instanceof Error ? error.message : "Unknown error"
}`,
{
status: 500,
}
);
}
}