요즘 AI가 유행이죠? 저도 알아요요
This commit is contained in:
parent
8399ce4107
commit
0017a3eb16
6 changed files with 122 additions and 15 deletions
|
|
@ -1,13 +1,14 @@
|
|||
import path from "path";
|
||||
import { join } from "path";
|
||||
import { createCanvas, loadImage, GlobalFonts } from "@napi-rs/canvas";
|
||||
import { getMealInfo } from "./meal"; // 이 함수의 내용은 제공되지 않았으므로 그대로 둡니다.
|
||||
import { getMealInfo, NameToEmoji } from "./meal"; // 이 함수의 내용은 제공되지 않았으므로 그대로 둡니다.
|
||||
import { getAllSchedules } from "./schedule";
|
||||
|
||||
GlobalFonts.registerFromPath('./template/Pretendard-Bold.ttf', 'Pretendard Bold')
|
||||
GlobalFonts.registerFromPath('./template/NotoColorEmoji-Regular.ttf', 'NotoColorEmoji Regular')
|
||||
|
||||
export class CreateImage {
|
||||
static async PostMeal(MLSV_YMD: string) {
|
||||
static async PostMeal(MLSV_YMD: string): Promise<void> {
|
||||
const mealInfo = await getMealInfo(MLSV_YMD);
|
||||
const img = await loadImage(path.join("./template/skeleton.png"));
|
||||
const canvas = createCanvas(img.width, img.height);
|
||||
|
|
@ -19,9 +20,23 @@ export class CreateImage {
|
|||
ctx.textAlign = "left";
|
||||
|
||||
const lines = mealInfo.meal.split("\n").reverse();
|
||||
lines.forEach((line, i) => {
|
||||
ctx.fillText(line, 75, 930 - i * 60);
|
||||
});
|
||||
let emojis = (await NameToEmoji(lines.toString())).split(",");
|
||||
console.log(emojis);
|
||||
if (lines.length !== emojis.length) {
|
||||
console.error("Error: Emojis and lines count mismatch retrying...");
|
||||
return this.PostMeal(MLSV_YMD);
|
||||
}
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const emoji = emojis[i];
|
||||
const text = lines[i];
|
||||
|
||||
ctx.font = "50px NotoColorEmoji Regular";
|
||||
ctx.fillText(emoji ?? "", 75, 930 - i * 60);
|
||||
|
||||
ctx.font = "56px Pretendard Bold";
|
||||
ctx.fillText(text ?? "", 75 + 80, 930 - i * 60);
|
||||
}
|
||||
|
||||
ctx.font = "24px Pretendard Bold";
|
||||
ctx.textAlign = "right";
|
||||
|
|
@ -138,7 +153,9 @@ export class CreateImage {
|
|||
|
||||
ctx.drawImage(img, offsetX, offsetY, resizedWidth, resizedHeight);
|
||||
|
||||
const outPath = path.join("./", `${filePath}-story.png`);
|
||||
const extIndex = filePath.lastIndexOf(".");
|
||||
const baseName = extIndex !== -1 ? filePath.substring(0, extIndex) : filePath;
|
||||
const outPath = path.join("./", `${baseName}-story.png`);
|
||||
const buffer = canvas.toBuffer("image/png");
|
||||
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue