feat: generate og image using templates

This commit is contained in:
tanishqmanuja
2023-09-20 15:48:31 +05:30
committed by Sat Naing
parent b5308debab
commit 3032c18321
2 changed files with 102 additions and 39 deletions
+15 -39
View File
@@ -1,8 +1,8 @@
import satori, { type SatoriOptions } from "satori"; import satori, { type SatoriOptions } from "satori";
import { writeFile } from "node:fs/promises";
import { Resvg } from "@resvg/resvg-js"; import { Resvg } from "@resvg/resvg-js";
import { getCollection } from "astro:content"; import { type CollectionEntry } from "astro:content";
import postOgImage from "./og-templates/post"; import postOgImage from "./og-templates/post";
import siteOgImage from "./og-templates/site";
const fetchFonts = async () => { const fetchFonts = async () => {
// Regular Font // Regular Font
@@ -42,42 +42,18 @@ const options: SatoriOptions = {
], ],
}; };
const generateOgImages = async () => { function svgBufferToPngBuffer(svg: string) {
const postImportResult = await getCollection( const resvg = new Resvg(svg);
"blog", const pngData = resvg.render();
({ data }) => !data.draft return pngData.asPng();
); }
const posts = Object.values(postImportResult).filter(
({ data }) => !data.ogImage
);
const imageGenerationPromises = posts.map(async post => { export async function generateOgImageForPost(post: CollectionEntry<"blog">) {
const svg = await satori(postOgImage(post.data.title), options); const svg = await satori(postOgImage(post), options);
return svgBufferToPngBuffer(svg);
}
// render png in production mode export async function generateOgImageForSite() {
if (import.meta.env.MODE === "production") { const svg = await satori(siteOgImage(), options);
const resvg = new Resvg(svg); return svgBufferToPngBuffer(svg);
const pngData = resvg.render(); }
const pngBuffer = pngData.asPng();
const outputImage = `${post.slug}.png`;
const LIGHT_BLUE = "\x1b[94m";
const DARK_GRAY = "\x1b[30m";
const RESET = "\x1b[0m";
console.info(
` ${LIGHT_BLUE}└─${RESET} output png image:`,
`${DARK_GRAY}/${outputImage}${RESET}`
);
await writeFile(`./dist/${outputImage}`, pngBuffer);
}
});
// Wait for all image generation operations to complete
await Promise.all(imageGenerationPromises);
return null;
};
export default generateOgImages;
+87
View File
@@ -0,0 +1,87 @@
import { SITE } from "@config";
export default () => {
return (
<div
style={{
background: "#fefbfb",
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<div
style={{
position: "absolute",
top: "-1px",
right: "-1px",
border: "4px solid #000",
background: "#ecebeb",
opacity: "0.9",
borderRadius: "4px",
display: "flex",
justifyContent: "center",
margin: "2.5rem",
width: "88%",
height: "80%",
}}
/>
<div
style={{
border: "4px solid #000",
background: "#fefbfb",
borderRadius: "4px",
display: "flex",
justifyContent: "center",
margin: "2rem",
width: "88%",
height: "80%",
}}
>
<div
style={{
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
margin: "20px",
width: "90%",
height: "90%",
}}
>
<div
style={{
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
height: "90%",
maxHeight: "90%",
overflow: "hidden",
textAlign: "center",
}}
>
<p style={{ fontSize: 72, fontWeight: "bold" }}>{SITE.title}</p>
<p style={{ fontSize: 28 }}>{SITE.desc}</p>
</div>
<div
style={{
display: "flex",
justifyContent: "flex-end",
width: "100%",
marginBottom: "8px",
fontSize: 28,
}}
>
<span style={{ overflow: "hidden", fontWeight: "bold" }}>
{new URL(SITE.website).hostname}
</span>
</div>
</div>
</div>
</div>
);
};