-
-
Save capJavert/7116c045d93d1ec6ee03d0ace17e7da9 to your computer and use it in GitHub Desktop.
Next.js API handler to convert and resize image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ImageResponse } from '@vercel/og' | |
import { NextRequest } from 'next/server' | |
const handler = async (req: NextRequest) => { | |
const [width, height] = [128, 128] | |
const { searchParams } = new URL(req.url) | |
const image = searchParams.get('image') | |
const fit = searchParams.get('fit') || 'none' | |
const imageResponse = new ImageResponse(( | |
<img | |
style={{ | |
width: `${width}px`, | |
height: `${height}px`, | |
objectFit: fit | |
}} | |
src={image} | |
/> | |
), { width, height }) | |
return imageResponse | |
} | |
export default handler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment