Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Custom Avatar API

Steven Tey03220c44c2d5
diff --git a/apps/web/app/api/og/avatar/route.tsx b/apps/web/app/api/og/avatar/route.tsx
new file mode 100644
index 0000000000..fb675c77d9
--- /dev/null
+++ b/apps/web/app/api/og/avatar/route.tsx
@@ -0,0 +1,57 @@
+import { ImageResponse } from "next/og";
+import { NextRequest } from "next/server";
+import { getTheme } from "./utils";
+
+export const runtime = "edge";
+
+export async function GET(req: NextRequest) {
+ const { searchParams } = new URL(req.url);
+ const seed = searchParams.get("seed");
+ const theme = getTheme(seed);
+
+ return new ImageResponse(
+ (
+ <div
+ tw="flex items-center justify-center w-full h-full relative"
+ style={{
+ background: theme.bg,
+ display: "flex",
+ margin: 0,
+ padding: 0,
+ }}
+ >
+ {/* Head */}
+ <div
+ tw="absolute w-[200px] h-[200px] rounded-full"
+ style={{
+ background: theme.fg,
+ display: "flex",
+ top: "110px",
+ backgroundImage:
+ "linear-gradient(135deg, rgba(255,255,255,0) 0%, rgba(0,0,0,0.2) 100%)",
+ boxShadow:
+ "inset 24px -20px 44px rgba(0,0,0,0.13), inset -72px -48px 76px rgba(255,255,255,0.4)",
+ }}
+ />
+ {/* Shoulders */}
+ <div
+ tw="absolute w-[400px] h-[400px] rounded-full"
+ style={{
+ background: theme.fg,
+ display: "flex",
+ top: "350px",
+ clipPath: "inset(0 0 50% 0)",
+ backgroundImage:
+ "linear-gradient(135deg, rgba(255,255,255,0) 0%, rgba(0,0,0,0.2) 100%)",
+ boxShadow:
+ "inset 40px -48px 76px rgba(0,0,0,0.4), inset -72px -48px 76px rgba(255,255,255,0.4), inset 8px -2px 44px rgba(0,0,0,0.1)",
+ }}
+ />
+ </div>
+ ),
+ {
+ width: 500,
+ height: 500,
+ },
+ );
+}
diff --git a/apps/web/app/api/og/avatar/themes.tsx b/apps/web/app/api/og/avatar/themes.tsx
new file mode 100644
index 0000000000..3bb3434a95
--- /dev/null
+++ b/apps/web/app/api/og/avatar/themes.tsx
@@ -0,0 +1,28 @@
+export const THEMES = [
+ // Blues
+ { bg: "#DBEAFE", fg: "#2B7FFF" },
+ { bg: "#DFF2FE", fg: "#00A6F4" },
+ // Greens
+ { bg: "#DCFCE7", fg: "#00C951" },
+ { bg: "#ECFCCA", fg: "#7CCF00" },
+ { bg: "#D0FAE5", fg: "#00BC7D" },
+ { bg: "#CBFBF1", fg: "#00BBA7" },
+ { bg: "#CEFAFE", fg: "#00B8DB" },
+ // Purples
+ { bg: "#E0E7FF", fg: "#615FFF" },
+ { bg: "#EDE9FE", fg: "#8E51FF" },
+ { bg: "#F3E8FF", fg: "#AD46FF" },
+ // Pinks
+ { bg: "#FAE8FF", fg: "#E12AFB" },
+ { bg: "#FCE7F3", fg: "#F6339A" },
+ // Reds
+ { bg: "#FFE2E2", fg: "#FB2C36" },
+ { bg: "#FFE4E6", fg: "#FF2056" },
+ // Oranges & Yellows
+ { bg: "#FFEDD4", fg: "#FF6900" },
+ { bg: "#FEF3C6", fg: "#FD9A00" },
+ { bg: "#FEF9C2", fg: "#EFB100" },
+ // Grays
+ { bg: "#F5F5F5", fg: "#404040" },
+ { bg: "#FAFAFA", fg: "#FAFAFA" },
+] as const;
diff --git a/apps/web/app/api/og/avatar/utils.ts b/apps/web/app/api/og/avatar/utils.ts
new file mode 100644
index 0000000000..232a8cbaa9
--- /dev/null
+++ b/apps/web/app/api/og/avatar/utils.ts
@@ -0,0 +1,23 @@
+import { THEMES } from "./themes";
+
+// Simple but effective hash function for strings
+function hashCode(str: string) {
+ let hash = 0;
+ for (let i = 0; i < str.length; i++) {
+ const char = str.charCodeAt(i);
+ hash = (hash << 5) - hash + char;
+ hash = hash & hash; // Convert to 32-bit integer
+ }
+ return Math.abs(hash);
+}
+
+// Get theme based on seed
+export function getTheme(seed?: string | null) {
+ if (!seed) {
+ // If no seed provided, return random theme
+ return THEMES[Math.floor(Math.random() * THEMES.length)];
+ }
+ // Use hash function to get deterministic index
+ const index = hashCode(seed) % THEMES.length;
+ return THEMES[index];
+}