-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base64 dynamic blur data URL on image
- Loading branch information
1 parent
ff8002d
commit b19f9d3
Showing
4 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
import NextImage, { ImageProps as NextImageProps } from 'next/image'; | ||
import { useState } from 'react'; | ||
import { twMerge } from 'tailwind-merge'; | ||
|
||
import { ImageFieldsFragment } from '@src/lib/__generated/sdk'; | ||
|
||
interface ImageProps extends Omit<ImageFieldsFragment, '__typename'> { | ||
nextImageProps?: Omit<NextImageProps, 'src' | 'alt'>; | ||
blurHash?: string; | ||
} | ||
|
||
export const CtfImage = ({ url, width, height, title, nextImageProps }: ImageProps) => { | ||
if (!url || !width || !height) return null; | ||
export const CtfImage = ({ url, width, height, title, nextImageProps, blurHash }: ImageProps) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
|
||
const blurURL = new URL(url); | ||
blurURL.searchParams.set('w', '10'); | ||
if (!url || !width || !height) return null; | ||
|
||
return ( | ||
<NextImage | ||
src={url} | ||
width={width} | ||
height={height} | ||
alt={title || ''} | ||
sizes="(max-width: 1200px) 100vw, 50vw" | ||
placeholder="blur" | ||
blurDataURL={blurURL.toString()} | ||
blurDataURL={blurHash} | ||
{...nextImageProps} | ||
className={twMerge(nextImageProps?.className, 'transition-all')} | ||
className={twMerge(nextImageProps?.className, `${isLoading ? 'blur-md ' : ''}transition-all`)} | ||
onLoad={() => setIsLoading(false)} | ||
priority | ||
/> | ||
); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const baseUrl = | ||
process.env.NODE_ENV === 'development' | ||
? 'http://localhost:3000/' | ||
: process.env.NEXT_PUBLIC_DOMAIN; | ||
|
||
export async function dynamicBlurDataUrl(url: string) { | ||
const base64str = await fetch(`${baseUrl}/_next/image?url=${url}&w=16&q=75`).then(async res => | ||
Buffer.from(await res.arrayBuffer()).toString('base64'), | ||
); | ||
|
||
const blurSvg = ` | ||
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 5'> | ||
<filter id='b' color-interpolation-filters='sRGB'> | ||
<feGaussianBlur stdDeviation='1' /> | ||
</filter> | ||
<image preserveAspectRatio='none' filter='url(#b)' x='0' y='0' height='100%' width='100%' | ||
href='data:image/avif;base64,${base64str}' /> | ||
</svg> | ||
`; | ||
|
||
const toBase64 = (str: string) => | ||
typeof window === 'undefined' ? Buffer.from(str).toString('base64') : window.btoa(str); | ||
|
||
return `data:image/svg+xml;base64,${toBase64(blurSvg)}`; | ||
} |