Skip to content

Commit

Permalink
feat: generate og images in png format
Browse files Browse the repository at this point in the history
In production mode, generate og images from svg to png format.

fix #40
  • Loading branch information
satnaing committed Mar 1, 2023
1 parent c40af43 commit 8197877
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 2 deletions.
5 changes: 5 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ export default defineConfig({
},
extendDefaultPlugins: true,
},
vite: {
optimizeDeps: {
exclude: ["@resvg/resvg-js"],
},
},
});
294 changes: 294 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@astrojs/rss": "^2.1.0",
"@resvg/resvg-js": "^2.4.1",
"astro": "^2.0.8",
"fuse.js": "^6.6.2",
"github-slugger": "^2.0.0",
Expand Down
20 changes: 18 additions & 2 deletions src/utils/generateOgImage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import satori, { SatoriOptions } from "satori";
import { SITE } from "@config";
import { writeFile } from "node:fs/promises";
import { Resvg } from "@resvg/resvg-js";

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

const generateOgImage = async (mytext = SITE.title) =>
await satori(ogImage(mytext), options);
const generateOgImage = async (mytext = SITE.title) => {
const svg = await satori(ogImage(mytext), 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();

console.info("Output PNG Image :", `${mytext}.png`);

await writeFile(`./dist/${mytext}.png`, pngBuffer);
}

return svg;
};

export default generateOgImage;

0 comments on commit 8197877

Please sign in to comment.