Skip to content

Commit

Permalink
feat: generate og image using templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqmanuja authored and satnaing committed Sep 21, 2023
1 parent fd50bc8 commit 0f82206
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 41 deletions.
58 changes: 17 additions & 41 deletions src/utils/generateOgImages.tsx
@@ -1,8 +1,8 @@
import satori, { type SatoriOptions } from "satori";
import { writeFile } from "node:fs/promises";
import { Resvg } from "@resvg/resvg-js";
import { getCollection } from "astro:content";
import { type CollectionEntry } from "astro:content";
import postOgImage from "./og-templates/post";
import siteOgImage from "./og-templates/site";

const fetchFonts = async () => {
// Regular Font
Expand Down Expand Up @@ -42,42 +42,18 @@ const options: SatoriOptions = {
],
};

const generateOgImages = async () => {
const postImportResult = await getCollection(
"blog",
({ data }) => !data.draft
);
const posts = Object.values(postImportResult).filter(
({ data }) => !data.ogImage
);

const imageGenerationPromises = posts.map(async post => {
const svg = await satori(postOgImage(post.data.title), options);

// render png in production mode
if (import.meta.env.MODE === "production") {
const resvg = new Resvg(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;
function svgBufferToPngBuffer(svg: string) {
const resvg = new Resvg(svg);
const pngData = resvg.render();
return pngData.asPng();
}

export async function generateOgImageForPost(post: CollectionEntry<"blog">) {
const svg = await satori(postOgImage(post), options);
return svgBufferToPngBuffer(svg);
}

export async function generateOgImageForSite() {
const svg = await satori(siteOgImage(), options);
return svgBufferToPngBuffer(svg);
}
87 changes: 87 additions & 0 deletions src/utils/og-templates/site.tsx
@@ -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>
);
};

0 comments on commit 0f82206

Please sign in to comment.