-
-
Save ssv445/b785a927d73d00e4452be9875b424e6d to your computer and use it in GitHub Desktop.
NextJS image loader for Directus
This file contains hidden or 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 type { ImageLoaderProps } from 'next/image' | |
| const imgDomain = process.env.NEXT_PUBLIC_IMG_DOMAIN ?? '' | |
| interface DirectusImageProps { | |
| fit?: 'cover' | 'contain' | 'inside' | 'outside' | |
| } | |
| export default function directusImageLoader({ | |
| src: imageID, | |
| width, | |
| quality, | |
| fit, | |
| }: ImageLoaderProps & DirectusImageProps) { | |
| const url = new URL(`https://${imgDomain}/assets/${imageID}`) | |
| url.searchParams.set('fit', fit ?? 'contain') | |
| url.searchParams.set('width', width.toString()) | |
| url.searchParams.set('quality', quality?.toString() || '75') | |
| return url.href | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment