Skip to content

Commit

Permalink
Add preview image generation for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
gnehs committed Feb 12, 2024
1 parent 1f0c143 commit 28adba1
Show file tree
Hide file tree
Showing 6 changed files with 758 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ jobs:
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
- name: Generate Session Preview Images
working-directory: ./utils/preview_generator
run: |
npm install
node index.mjs
- name: Install npm packages
run: npm install
- name: Build Pages
Expand Down
Binary file added utils/preview_generator/fonts/NotoSansTC-Bold.otf
Binary file not shown.
Binary file not shown.
120 changes: 120 additions & 0 deletions utils/preview_generator/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import satori from "satori";
import fs from "fs";
import { Resvg } from "@resvg/resvg-js";
const sessions = JSON.parse(fs.readFileSync("../../public/sessions.json"));
const NotoSansTCBold = fs.readFileSync("./fonts/NotoSansTC-Bold.otf");
const NotoSansTCMedium = fs.readFileSync("./fonts/NotoSansTC-Medium.otf");
const logoImage = fs.readFileSync("../../public/logo_2024.png");
for (let session of sessions.sessions) {
const title = session.zh.title;
const speakers = session.speakers
.map((x) => sessions.speakers.find((speaker) => speaker.id == x)?.zh.name)
.filter((x) => x)
.join("、");
if (session.zh.description == "") continue;
console.time(session.zh.title);
const svg = await satori(
{
type: "div",
props: {
style: {
display: "flex",
flexDirection: "column",
backgroundColor: "#F8F3E8",
color: "#385AAC",
padding: 64,
height: 600,
width: 1200,
},
children: [
{
type: "div",
props: {
children: "Presentation",
style: {
fontSize: 32,
},
},
},
{
type: "div",
props: {
children: title,
style: {
fontSize: 64,
fontWeight: 700,
},
},
},
{
type: "div",
props: {
style: {
flex: 1,
},
},
},
{
type: "div",
props: {
style: {
display: "flex",
justifyContent: "space-between",
alignItems: "flex-end",
},
children: [
{
type: "div",
props: {
children: speakers,
style: {
fontSize: 48,
},
},
},
{
type: "img",
props: {
height: 128,
src: `data:image/png;base64,${Buffer.from(
logoImage,
).toString("base64")}`,
},
},
],
},
},
],
},
},
{
width: 1200,
height: 600,
fonts: [
{
name: "Noto Sans TC",
data: NotoSansTCMedium,
weight: 500,
style: "normal",
},
{
name: "Noto Sans TC",
data: NotoSansTCBold,
weight: 700,
style: "normal",
},
],
},
);
const resvg = new Resvg(svg, {
fitTo: {
mode: "width",
value: 1200,
},
});
const pngData = resvg.render();
const pngBuffer = pngData.asPng();
fs.mkdirSync("../../public/sessions", { recursive: true });
fs.writeFileSync(`../../public/sessions/${session.id}.png`, pngBuffer);
console.timeEnd(session.zh.title);
}
Loading

0 comments on commit 28adba1

Please sign in to comment.