File size: 2,604 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import isAnimated from 'next/dist/compiled/is-animated'
import { optimizeImage } from '../../../../server/image-optimizer'

const BLUR_IMG_SIZE = 8
const BLUR_QUALITY = 70
const VALID_BLUR_EXT = ['jpeg', 'png', 'webp', 'avif'] // should match other usages

export async function getBlurImage(
  content: Buffer,
  extension: string,
  imageSize: { width: number; height: number },
  {
    basePath,
    outputPath,
    isDev,
    tracing = () => ({
      traceFn:
        (fn) =>
        (...args: any) =>
          fn(...args),
      traceAsyncFn:
        (fn) =>
        (...args: any) =>
          fn(...args),
    }),
  }: {
    basePath: string
    outputPath: string
    isDev: boolean
    tracing: (name?: string) => {
      traceFn(fn: Function): any
      traceAsyncFn(fn: Function): any
    }
  }
) {
  let blurDataURL: string | undefined
  let blurWidth: number = 0
  let blurHeight: number = 0

  if (VALID_BLUR_EXT.includes(extension) && !isAnimated(content)) {
    // Shrink the image's largest dimension
    if (imageSize.width >= imageSize.height) {
      blurWidth = BLUR_IMG_SIZE
      blurHeight = Math.max(
        Math.round((imageSize.height / imageSize.width) * BLUR_IMG_SIZE),
        1
      )
    } else {
      blurWidth = Math.max(
        Math.round((imageSize.width / imageSize.height) * BLUR_IMG_SIZE),
        1
      )
      blurHeight = BLUR_IMG_SIZE
    }

    if (isDev) {
      // During `next dev`, we don't want to generate blur placeholders with webpack
      // because it can delay starting the dev server. Instead, we inline a
      // special url to lazily generate the blur placeholder at request time.
      const prefix = 'http://localhost'
      const url = new URL(`${basePath || ''}/_next/image`, prefix)
      url.searchParams.set('url', outputPath)
      url.searchParams.set('w', String(blurWidth))
      url.searchParams.set('q', String(BLUR_QUALITY))
      blurDataURL = url.href.slice(prefix.length)
    } else {
      const resizeImageSpan = tracing('image-resize')
      const resizedImage = await resizeImageSpan.traceAsyncFn(() =>
        optimizeImage({
          buffer: content,
          width: blurWidth,
          height: blurHeight,
          contentType: `image/${extension}`,
          quality: BLUR_QUALITY,
        })
      )
      const blurDataURLSpan = tracing('image-base64-tostring')
      blurDataURL = blurDataURLSpan.traceFn(
        () =>
          `data:image/${extension};base64,${resizedImage.toString('base64')}`
      )
    }
  }
  return {
    dataURL: blurDataURL,
    width: blurWidth,
    height: blurHeight,
  }
}