Skip to content

Commit 5c5e691

Browse files
committed
✨ add thumbnail
1 parent 60cbd99 commit 5c5e691

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

api/_lib/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ParsedRequest, Theme } from './types';
55
export function parseRequest(req: IncomingMessage) {
66
console.log('HTTP ' + req.url);
77
const { pathname, query } = parse(req.url || '/', true);
8-
const { fontSize, images, widths, heights, theme, md, description } = (query || {});
8+
const { description, thumbnail, fontSize, images, widths, heights, theme, md } = (query || {});
99

1010
if (Array.isArray(fontSize)) {
1111
throw new Error('Expected a single fontSize');
@@ -16,6 +16,9 @@ export function parseRequest(req: IncomingMessage) {
1616
if (Array.isArray(description)) {
1717
throw new Error('Expected a single description');
1818
}
19+
if (Array.isArray(thumbnail)) {
20+
throw new Error('Expected a single description');
21+
}
1922

2023
const arr = (pathname || '/').slice(1).split('.');
2124
let extension = '';
@@ -33,6 +36,7 @@ export function parseRequest(req: IncomingMessage) {
3336
fileType: extension === 'jpeg' ? extension : 'png',
3437
title: decodeURIComponent(text),
3538
description: decodeURIComponent(getString(description)),
39+
thumbnail: getString(thumbnail),
3640
theme: (theme === 'light' || theme === 'black' || theme === 'dark' || theme === 'expat' || theme === 'night') ? theme : 'light',
3741
md: md === '1' || md === 'true',
3842
fontSize: fontSize || '96px',

api/_lib/template.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const emojify = (text: string) => twemoji.parse(text, twOptions);
88

99
const mono = readFileSync(`${__dirname}/../_fonts/Vera-Mono.woff2`).toString('base64');
1010

11-
function getCss(theme: Theme, fontSize: string) {
11+
function getCss(theme: Theme, fontSize: string, thumbnail?: string) {
1212
let background = 'white';
1313
let foreground = 'black';
1414

@@ -31,6 +31,8 @@ function getCss(theme: Theme, fontSize: string) {
3131
break;
3232
}
3333

34+
if(thumbnail) background = `linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, ${background} 100%), url(${thumbnail});`;
35+
3436
return `
3537
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap');
3638
@@ -47,6 +49,9 @@ function getCss(theme: Theme, fontSize: string) {
4749
4850
body {
4951
background: ${background};
52+
background-size: cover;
53+
background-position: center;
54+
background-repeat: no-repeat;
5055
height: 100vh;
5156
display: flex;
5257
flex-direction: column;
@@ -124,14 +129,14 @@ function getCss(theme: Theme, fontSize: string) {
124129
}
125130

126131
export function getHtml(parsedReq: ParsedRequest) {
127-
const { title, description, theme, md, fontSize, images, widths, heights } = parsedReq;
132+
const { title, description, thumbnail, theme, md, fontSize, images, widths, heights } = parsedReq;
128133
return `<!DOCTYPE html>
129134
<html>
130135
<meta charset="utf-8">
131136
<title>Generated Image</title>
132137
<meta name="viewport" content="width=device-width, initial-scale=1">
133138
<style>
134-
${getCss(theme, fontSize)}
139+
${getCss(theme, fontSize, thumbnail)}
135140
</style>
136141
<body>
137142
<div class="container">

api/_lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface ParsedRequest {
55
fileType: FileType;
66
title: string;
77
description: string;
8+
thumbnail: string;
89
theme: Theme;
910
md: boolean;
1011
fontSize: string;

web/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const App = (_: any, state: AppState, setState: SetState) => {
192192
md = true,
193193
title = 'onRuntime Studio',
194194
description = 'Enjoy this tool from **onRuntime Studio**!',
195+
thumbnail = 'https://picsum.photos/1920/1080',
195196
images=[imageLightOptions[0].value],
196197
widths=[],
197198
heights=[],
@@ -207,6 +208,7 @@ const App = (_: any, state: AppState, setState: SetState) => {
207208
const url = new URL(window.location.origin);
208209
url.pathname = `${encodeURIComponent(title)}.${fileType}`;
209210
url.searchParams.append('description', encodeURIComponent(description));
211+
url.searchParams.append('thumbnail', thumbnail);
210212
url.searchParams.append('theme', theme);
211213
url.searchParams.append('md', mdValue);
212214
url.searchParams.append('fontSize', fontSize);
@@ -282,6 +284,16 @@ const App = (_: any, state: AppState, setState: SetState) => {
282284
}
283285
})
284286
}),
287+
H(Field, {
288+
label: 'Thumbnail Input',
289+
input: H(TextInput, {
290+
value: thumbnail,
291+
oninput: (val: string) => {
292+
console.log('oninput ' + val, url);
293+
setLoadingState({ thumbnail: val, overrideUrl: url });
294+
}
295+
})
296+
}),
285297
H(Field, {
286298
label: 'Image 1',
287299
input: H('div',

0 commit comments

Comments
 (0)