Skip to content

Commit 5f2b454

Browse files
author
Giannis Chatziveroglou
committed
Add discriminator for discord image
1 parent fa743c7 commit 5f2b454

3 files changed

Lines changed: 50 additions & 25 deletions

File tree

api/img-generator/generator.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function getImage(
1919
nameParam: string,
2020
imgUri: string,
2121
textParam: string,
22-
cluster: string | null
22+
cluster: string | null,
23+
proxy?: string
2324
): Promise<Buffer> {
2425
console.log(
2526
`Handling img generatation for mintId (${mintId}) imgUri (${imgUri}) text (${textParam}) and cluster (${
@@ -42,8 +43,8 @@ export async function getImage(
4243
!tokenData.useInvalidatorData &&
4344
cluster !== "devnet"
4445
) {
45-
console.log("Falling back to devnet metadata");
46-
return getImage(mintId, nameParam, imgUri, textParam, "devnet");
46+
console.log("Falling back to devnet image");
47+
return getImage(mintId, nameParam, imgUri, textParam, "devnet", proxy);
4748
}
4849

4950
const originalMint = tokenData?.certificateData?.parsed
@@ -62,9 +63,12 @@ export async function getImage(
6263
}
6364
}
6465

66+
const parsedProxy = proxy ? proxy === "true" : false;
67+
console.log("parsedProxy", parsedProxy, proxy);
6568
if (
6669
tokenData?.metaplexData?.parsed.data.symbol === "NAME" ||
67-
(textParam && textParam.includes("@"))
70+
(textParam && textParam.includes("@")) ||
71+
parsedProxy
6872
) {
6973
const mintName =
7074
originalTokenData?.metaplexData?.parsed.data.name ||
@@ -77,7 +81,7 @@ export async function getImage(
7781
: breakIdentity(mintName || textParam || "")[1];
7882

7983
if (namespace && IDENTITIES.includes(namespace)) {
80-
return getIdentityImage(namespace, entryName);
84+
return getIdentityImage(namespace, entryName, parsedProxy);
8185
} else {
8286
try {
8387
const data = await promises.readFile(

api/img-generator/handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports.generate = async (event) => {
77
event.queryStringParameters && event.queryStringParameters.name,
88
event.queryStringParameters && event.queryStringParameters.uri,
99
event.queryStringParameters && event.queryStringParameters.text,
10-
event.queryStringParameters && event.queryStringParameters.cluster
10+
event.queryStringParameters && event.queryStringParameters.cluster,
11+
event.queryStringParameters && event.queryStringParameters.proxy
1112
);
1213

1314
console.log("Returning image buffer", buffer);

api/img-generator/identity-image.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const IDENTITY_COLORS: { [key: string]: string } = {
66
discord: "#5866f2",
77
};
88

9-
export async function getIdentityImage(namespace: string, handle: string) {
10-
console.log(`Rending ${namespace} image`);
9+
export async function getIdentityImage(
10+
namespace: string,
11+
handle: string,
12+
proxy?: boolean
13+
) {
14+
console.log(`Rendering ${namespace} image`);
1115

1216
// setup
1317
canvas.registerFont(__dirname.concat("/fonts/SF-Pro.ttf"), {
@@ -29,7 +33,13 @@ export async function getIdentityImage(namespace: string, handle: string) {
2933
nameCtx.textAlign = "center";
3034
nameCtx.textBaseline = "middle";
3135

32-
const nameText = formatName(namespace, handle);
36+
let nameText = decodeURIComponent(formatName(namespace, handle));
37+
let topRightText: string | undefined;
38+
if (namespace === "discord") {
39+
const temp = nameText.split("#");
40+
nameText = temp.slice(0, -1).join();
41+
topRightText = temp.pop();
42+
}
3343
nameCtx.fillText(nameText, WIDTH * 0.5, HEIGHT * 0.5);
3444
nameCtx.textAlign = "left";
3545

@@ -46,26 +56,28 @@ export async function getIdentityImage(namespace: string, handle: string) {
4656
HEIGHT * 0.18
4757
);
4858

49-
const bottomLeftCtx = imageCanvas.getContext("2d");
50-
bottomLeftCtx.textAlign = "left";
51-
let bottomLeft = PADDING * 1.5;
59+
if (!proxy) {
60+
const bottomLeftCtx = imageCanvas.getContext("2d");
61+
bottomLeftCtx.textAlign = "left";
62+
let bottomLeft = PADDING * 1.5;
5263

53-
bottomLeftCtx.font = `${0.055 * WIDTH}px SFPro`;
54-
bottomLeftCtx.fillStyle = "white";
55-
bottomLeftCtx.drawImage(
56-
await canvas.loadImage(__dirname.concat("/assets/infinity.png")),
57-
PADDING,
58-
HEIGHT - bottomLeft - 0.08 * WIDTH,
59-
0.15 * WIDTH,
60-
0.15 * WIDTH
61-
);
62-
bottomLeft += 0.075 * WIDTH;
64+
bottomLeftCtx.font = `${0.055 * WIDTH}px SFPro`;
65+
bottomLeftCtx.fillStyle = "white";
66+
bottomLeftCtx.drawImage(
67+
await canvas.loadImage(__dirname.concat("/assets/infinity.png")),
68+
PADDING,
69+
HEIGHT - bottomLeft - 0.08 * WIDTH,
70+
0.15 * WIDTH,
71+
0.15 * WIDTH
72+
);
73+
bottomLeft += 0.075 * WIDTH;
74+
}
6375

64-
const topLextCtx = imageCanvas.getContext("2d");
76+
const topLeftCtx = imageCanvas.getContext("2d");
6577
let topLeft = PADDING;
6678
if (IDENTITIES.includes(namespace)) {
6779
if (namespace === "twitter") {
68-
topLextCtx.drawImage(
80+
topLeftCtx.drawImage(
6981
await canvas.loadImage(
7082
__dirname.concat("/assets/twitter-white-logo.png")
7183
),
@@ -75,7 +87,7 @@ export async function getIdentityImage(namespace: string, handle: string) {
7587
0.15 * HEIGHT
7688
);
7789
} else if (namespace === "discord") {
78-
topLextCtx.drawImage(
90+
topLeftCtx.drawImage(
7991
await canvas.loadImage(__dirname.concat("/assets/discord-logo.png")),
8092
topLeft,
8193
PADDING,
@@ -86,6 +98,14 @@ export async function getIdentityImage(namespace: string, handle: string) {
8698
topLeft += 0.11 * WIDTH;
8799
}
88100

101+
if (topRightText) {
102+
const topRightCtx = imageCanvas.getContext("2d");
103+
topRightCtx.font = `${0.08 * WIDTH}px SFPro`;
104+
topRightCtx.fillStyle = "white";
105+
topRightCtx.textAlign = "right";
106+
topRightCtx.fillText("#" + topRightText, WIDTH * 0.95, HEIGHT * 0.1);
107+
}
108+
89109
const buffer = imageCanvas.toBuffer("image/png");
90110
return buffer;
91111
}

0 commit comments

Comments
 (0)