iamseeley-beigemandrill.web.val.run
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
export default async function (req: Request): Promise<Response> {
const { searchParams } = new URL(req.url);
// Default values
const title = searchParams.get('title') || 'Hello from val.town';
const bgColor = searchParams.get('bgColor') || '#e15a24';
const textColor = searchParams.get('textColor') || '#1A202C';
// Create SVG
const svg = `
<svg width="1200" height="630" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
@import url('https://fonts.googleapis.com/css2?family=Aleo:wght@700&amp;display=swap');
</style>
</defs>
<rect width="1200" height="630" fill="${bgColor}" />
<text x="600" y="315" font-family="Aleo, serif" font-size="60" font-weight="bold" fill="${textColor}" text-anchor="middle" dominant-baseline="middle">${title}</text>
</svg>
`;
// Retrieve HTCI API key from environment variables
const apiUrl = 'https://hcti.io/v1/image';
const apiKey = Deno.env.get("HTCI");
if (!apiKey) {
console.error('HTCI API key is missing');
return new Response('Internal Server Error', { status: 500 });
}
console.log('HTCI API Key:', apiKey);
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
html: svg,
device: { width: 1200, height: 630 },
}),
});
if (!response.ok) {
throw new Error(`Conversion failed: ${response.statusText}`);
}
const jsonResponse = await response.json();
const pngUrl = jsonResponse.url;
const pngResponse = await fetch(pngUrl);
const pngBuffer = await pngResponse.arrayBuffer();
return new Response(pngBuffer, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=604800, immutable'
},
});
} catch (error) {
console.error('Error converting SVG to PNG:', error);
return new Response('Error generating image', { status: 500 });
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 18, 2024