-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
62 lines (54 loc) · 1.48 KB
/
index.js
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
import sharp from 'sharp'
import pMap from 'p-map'
export default function lqipModern(input, opts = {}) {
const { concurrency = 4, ...rest } = opts
if (Array.isArray(input)) {
return pMap(input, async (image) => computeLqipImage(image, rest), {
concurrency
})
} else {
return computeLqipImage(input, opts)
}
}
async function computeLqipImage(input, opts = {}) {
const { resize = 16, outputFormat = 'webp', outputOptions } = opts
const image = sharp(input).rotate()
const metadata = await image.metadata()
const resized = image.resize(
...(Array.isArray(resize)
? resize
: [
Math.min(metadata.width, resize),
Math.min(metadata.height, resize),
{ fit: 'inside' }
])
)
let output
if (outputFormat === 'webp') {
output = resized.webp({
quality: 20,
alphaQuality: 20,
smartSubsample: true,
...outputOptions
})
} else if (outputFormat === 'jpg' || outputFormat === 'jpeg') {
output = resized.jpeg({
quality: 20,
...outputOptions
})
} else {
throw new Error(`Invalid outputformat "${outputFormat}"`)
}
const { data, info } = await output.toBuffer({ resolveWithObject: true })
return {
content: data,
metadata: {
originalWidth: metadata.width,
originalHeight: metadata.height,
width: info.width,
height: info.height,
type: outputFormat,
dataURIBase64: `data:image/webp;base64,${data.toString('base64')}`
}
}
}