Skip to content

Commit

Permalink
Merge pull request #416 from ourzora/fix-svg-rendering
Browse files Browse the repository at this point in the history
Fix svgs and dashboard image
  • Loading branch information
neokry authored Nov 28, 2023
2 parents 4473680 + 1e5298c commit 0752ed7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions apps/web/src/modules/dashboard/DaoAuctionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const DaoAuctionCard = (props: DaoAuctionCardProps) => {
<Image
className={daoAvatar}
src={currentAuction?.token?.image}
unoptimized
layout="fixed"
alt=""
/>
Expand Down
31 changes: 17 additions & 14 deletions apps/web/src/pages/api/renderer/stack-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,41 @@ import sharp from 'sharp'

import { CACHE_TIMES } from 'src/constants/cacheTimes'

const SVG_DEFAULT_SIZE = 1080

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const images = req.query.images
let images = req.query.images

if (!images || !images.length)
return res.status(400).json({ error: 'No images provided' })

const { maxAge, swr } = CACHE_TIMES.DAO_FEED

// Handle single image
if (typeof images === 'string') {
const data = await getImageData(images)
const convertedImage = await sharp(data).webp({ quality: 100 }).toBuffer()
if (typeof images === 'string') images = [images]

let imageData: Buffer[] = await Promise.all(
images.map((imageUrl) => getImageData(imageUrl))
)

res.setHeader(
'Cache-Control',
`public, s-maxage=${maxAge}, stale-while-revalidate=${swr}`
const isSvg = images[0].includes('.svg')

// Resize all SVGs to a default size
if (isSvg) {
imageData = await Promise.all(
imageData.map(async (x) =>
sharp(x).resize(SVG_DEFAULT_SIZE, SVG_DEFAULT_SIZE, { fit: 'inside' }).toBuffer()
)
)
res.setHeader('Content-Type', 'image/webp')
return res.send(convertedImage)
}

// Handle multiple images
const imageData = await Promise.all(images.map((imageUrl) => getImageData(imageUrl)))

const compositeParams = imageData.slice(1).map((x) => ({
input: x,
gravity: 'center',
}))

const compositeRes = await sharp(imageData[0])
.composite(compositeParams)
.webp({ quality: 100 })
.webp({ quality: 75 })
.toBuffer()

res.setHeader(
Expand Down

2 comments on commit 0752ed7

@vercel
Copy link

@vercel vercel bot commented on 0752ed7 Nov 28, 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:

testnet-nouns-builder – ./apps/web

testnet.nouns.build
testnet-nouns-builder-nouns-builder.vercel.app
testnet-nouns-builder-git-main-nouns-builder.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 0752ed7 Nov 28, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.