Skip to content

Commit 898dbbd

Browse files
refactor(template): revise color template blur (#35)
1 parent 58ad673 commit 898dbbd

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/lib/components/image-templates/ColorTemplate.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import type { OgImageOption } from "lib/types/ogImageOption";
66

77
type ColorTemplateProps = Omit<OgImageOption, "template">;
88

9-
const ColorTemplate = ({ heading, text, center }: ColorTemplateProps) => {
9+
const ColorTemplate = ({
10+
heading,
11+
text,
12+
center,
13+
width,
14+
height,
15+
}: ColorTemplateProps) => {
16+
const aHeight = height ?? 0;
17+
const aWidth = width ?? 0;
18+
const blurSize = (aWidth < aHeight ? aWidth : aHeight) / 3.2;
19+
1020
return (
1121
<div tw="w-screen h-screen flex flex-col justify-center bg-gray-900">
1222
<div
1323
style={{
1424
position: "absolute",
15-
height: "150%",
16-
width: "150%",
17-
transform: "translate(-50%)",
18-
filter: "blur(200px) saturate(150%)",
25+
height: `${aHeight.toString()}px`,
26+
width: `${aWidth.toString()}px`,
27+
filter: `blur(${blurSize}px) saturate(150%)`,
1928
backgroundImage: `linear-gradient(45deg, #f97316, #06b6d4)`,
2029
}}
2130
/>

src/lib/components/image-templates/TemplateWrapper.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const TemplateSwitcher = ({
1313
text,
1414
template,
1515
center,
16+
width,
17+
height,
1618
}: TemplateSwitcherProps) => {
1719
if (template === PLAIN_TEMPLATE) {
18-
return <BaseTemplate {...{ heading, text }} />;
20+
return <BaseTemplate {...{ heading, text, width, height }} />;
1921
}
2022

21-
return <ColorTemplate {...{ heading, text, center }} />;
23+
return <ColorTemplate {...{ heading, text, center, width, height }} />;
2224
};
2325

2426
export default TemplateSwitcher;

src/lib/types/ogImageOption.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ export type OgImageOption = {
33
text?: string;
44
template?: string;
55
center?: boolean;
6+
width?: number;
7+
height?: number;
68
};
79

8-
export type OgImageOptionConverted = Omit<OgImageOption, "center"> & {
9-
center?: string;
10+
export type OgImageOptionConverted = {
11+
[key in keyof OgImageOption]?: string;
1012
};

src/lib/utils/buildOgImageUrl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const buildOgImageUrl = (options: OgImageOption) => {
1010
const converted: OgImageOptionConverted = {
1111
...options,
1212
center: options.center ? String(options.center) : undefined,
13+
width: options.width?.toString(),
14+
height: options.height?.toString(),
1315
};
1416
const purgedOptions = pickBy(converted);
1517
const urlParams = new URLSearchParams(purgedOptions).toString();

src/pages/api/generate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function handler(req: NextRequest) {
2020
const center = Boolean(searchParams.get("center"));
2121
const width = Number(searchParams.get("width") ?? 1200);
2222
const height = Number(searchParams.get("height") ?? 630);
23-
const templateProps = { heading, text, template, center };
23+
const templateProps = { heading, text, template, center, width, height };
2424

2525
return new ImageResponse(<TemplateSwitcher {...templateProps} />, {
2626
width,

0 commit comments

Comments
 (0)