Skip to content

Commit

Permalink
Add discriminator for discord image
Browse files Browse the repository at this point in the history
  • Loading branch information
Giannis Chatziveroglou committed Jul 5, 2022
1 parent fa743c7 commit 9757362
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/img-generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function getImage(
!tokenData.useInvalidatorData &&
cluster !== "devnet"
) {
console.log("Falling back to devnet metadata");
console.log("Falling back to devnet image");
return getImage(mintId, nameParam, imgUri, textParam, "devnet");
}

Expand Down
24 changes: 19 additions & 5 deletions api/img-generator/identity-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const IDENTITY_COLORS: { [key: string]: string } = {
};

export async function getIdentityImage(namespace: string, handle: string) {
console.log(`Rending ${namespace} image`);
console.log(`Rendering ${namespace} image`);

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

const nameText = formatName(namespace, handle);
let nameText = decodeURIComponent(formatName(namespace, handle));
let topRightText: string | undefined;
if (namespace === "discord") {
const temp = nameText.split(">");
nameText = temp.slice(0, -1).join();
topRightText = temp.pop();
}
nameCtx.fillText(nameText, WIDTH * 0.5, HEIGHT * 0.5);
nameCtx.textAlign = "left";

Expand Down Expand Up @@ -61,11 +67,11 @@ export async function getIdentityImage(namespace: string, handle: string) {
);
bottomLeft += 0.075 * WIDTH;

const topLextCtx = imageCanvas.getContext("2d");
const topLeftCtx = imageCanvas.getContext("2d");
let topLeft = PADDING;
if (IDENTITIES.includes(namespace)) {
if (namespace === "twitter") {
topLextCtx.drawImage(
topLeftCtx.drawImage(
await canvas.loadImage(
__dirname.concat("/assets/twitter-white-logo.png")
),
Expand All @@ -75,7 +81,7 @@ export async function getIdentityImage(namespace: string, handle: string) {
0.15 * HEIGHT
);
} else if (namespace === "discord") {
topLextCtx.drawImage(
topLeftCtx.drawImage(
await canvas.loadImage(__dirname.concat("/assets/discord-logo.png")),
topLeft,
PADDING,
Expand All @@ -86,6 +92,14 @@ export async function getIdentityImage(namespace: string, handle: string) {
topLeft += 0.11 * WIDTH;
}

if (topRightText) {
const topRightCtx = imageCanvas.getContext("2d");
topRightCtx.font = `${0.08 * WIDTH}px SFPro`;
topRightCtx.fillStyle = "white";
topRightCtx.textAlign = "right";
topRightCtx.fillText("#" + topRightText, WIDTH * 0.95, HEIGHT * 0.1);
}

const buffer = imageCanvas.toBuffer("image/png");
return buffer;
}

0 comments on commit 9757362

Please sign in to comment.