-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageGenerator.ts
98 lines (84 loc) · 2.22 KB
/
ImageGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import axios from "axios";
import fs from "fs";
import { createCanvas, loadImage } from "canvas";
import FormData from "form-data";
export interface IpfsImage {
cid: string;
fullUrl: string;
}
export interface Config {
depth: number;
bgColors: { [key: number]: string };
textColors: { [key: number]: string };
}
const nestedConfig = {
depth: 5,
bgColors: {
0: "#FFFFFF",
1: "#0558FE",
2: "#05FE5C",
3: "#EFFE05",
4: "#800080",
5: "#FE2E05",
},
textColors: {
0: "#000000",
1: "#3C6A69",
2: "#3C506A",
3: "#4B3C6A",
4: "#6A3C68",
5: "#6A3C3C",
},
} as Config;
// TODO: env
const uploadUrl = "https://rest.dev.uniquenetwork.dev/v1/ipfs/upload-file";
class ImageGenerator {
// prefix - parent.name
public async generateImage(
depth: number,
index: number,
prefix: string
): Promise<IpfsImage> {
const bgColor = nestedConfig.bgColors[depth];
const textColor = nestedConfig.textColors[index];
// set up scene
const width = 240;
const height = 100;
const fontSize = 40;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
ctx.fillStyle = bgColor;
ctx.beginPath();
ctx.fillRect(0, 0, width, height);
ctx.fill();
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = "#000000";
ctx.fillText(prefix, fontSize / 2, height - fontSize);
ctx.fillStyle = textColor;
ctx.fillText(
`-${index + 1}`,
ctx.measureText(prefix).width + fontSize / 2,
height - fontSize
);
const catImage = await loadImage("./cat.png");
ctx.drawImage(catImage, width - 50, height - 50, 50, 50);
const buffer = canvas.toBuffer();
// fs.writeFileSync(`./nested-images/${depth}/${index}.png`, buffer);
// return { cid: '', fullUrl: '' }
return await this.uploadImage(buffer);
}
public async uploadImage(file: Buffer): Promise<IpfsImage> {
const form = new FormData();
form.append("file", file, "file");
const response = await axios({
method: "post",
url: uploadUrl,
data: form,
headers: {
"Content-Type": `multipart/form-data`,
},
});
return response.data as IpfsImage;
}
}
export default new ImageGenerator();