Skip to content

Commit

Permalink
feat: og random color
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 18, 2023
1 parent 60ee1aa commit 983d090
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"shiki": "0.14.3",
"socket.io-client": "4.7.1",
"tailwind-merge": "1.14.0",
"uniqolor": "1.1.0",
"xss": "1.0.14"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions src/app/api/og/route.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uniqolor from 'uniqolor'
import type { AggregateRoot } from '@mx-space/api-client'
import type { NextRequest } from 'next/server'

Expand All @@ -6,7 +7,7 @@ import { ImageResponse } from '@vercel/og'
import { apiClient } from '~/lib/request'

const fontNormal = fetch(
'https://github.com/lxgw/kose-font/raw/master/TTF%20(Simplified%20Chinese)/XiaolaiSC-Regular.ttf',
'https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf',
).then((res) => res.arrayBuffer())
export const runtime = 'edge'

Expand All @@ -18,7 +19,7 @@ export const GET = async (req: NextRequest) => {
const { searchParams } = req.nextUrl

const titlePost = searchParams.get('title')
const subtitlePost = searchParams.get('subtitle')
const subtitlePost = searchParams.get('subtitle') || ''

const aggregation = await fetch(apiClient.aggregate.proxy.toString(true), {
next: {
Expand All @@ -31,6 +32,27 @@ export const GET = async (req: NextRequest) => {
seo: { title },
} = aggregation

if (!title)
return new Response(
'Failed to generate the OG image. Error: The title is required.',
{ status: 400 },
)

const bgAccent = uniqolor(titlePost + subtitlePost, {
saturation: [30, 35],
lightness: [60, 70],
}).color

const bgAccentLight = uniqolor(titlePost + subtitlePost, {
saturation: [30, 35],
lightness: [80, 90],
}).color

const bgAccentUltraLight = uniqolor(titlePost + subtitlePost, {
saturation: [30, 35],
lightness: [95, 96],
}).color

return new ImageResponse(
(
<div
Expand All @@ -39,10 +61,9 @@ export const GET = async (req: NextRequest) => {
height: '100%',
width: '100%',

background:
'linear-gradient(37deg, #66BDB3 47.82%, #C0E3DD 79.68%, #F2F9F5 100%)',
background: `linear-gradient(37deg, ${bgAccent} 27.82%, ${bgAccentLight} 79.68%, ${bgAccentUltraLight} 100%)`,

fontFamily: 'Xiaolai',
fontFamily: 'LXGW WenKai Screen R',

padding: '5rem',
alignItems: 'flex-end',
Expand Down Expand Up @@ -117,7 +138,7 @@ export const GET = async (req: NextRequest) => {
height: 600,
fonts: [
{
name: 'Xiaolai',
name: 'LXGW WenKai Screen R',
data: fontData,
weight: 400,
style: 'normal',
Expand Down

1 comment on commit 983d090

@vercel
Copy link

@vercel vercel bot commented on 983d090 Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

springtide.vercel.app
shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
innei.in

Please sign in to comment.