Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix svgs and dashboard image #416

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading