Skip to content

Instantly share code, notes, and snippets.

@capJavert
Last active January 2, 2024 12:54
Show Gist options
  • Save capJavert/7116c045d93d1ec6ee03d0ace17e7da9 to your computer and use it in GitHub Desktop.
Save capJavert/7116c045d93d1ec6ee03d0ace17e7da9 to your computer and use it in GitHub Desktop.
Next.js API handler to convert and resize image
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