-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathcloudflare.ts
More file actions
55 lines (49 loc) · 1.22 KB
/
cloudflare.ts
File metadata and controls
55 lines (49 loc) · 1.22 KB
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
import { encodeQueryItem, joinURL } from 'ufo'
import { createOperationsGenerator } from '../utils/index'
import { defineProvider } from '../utils/provider'
const operationsGenerator = createOperationsGenerator({
keyMap: {
width: 'w',
height: 'h',
dpr: 'dpr',
fit: 'fit',
gravity: 'g',
quality: 'q',
format: 'f',
sharpen: 'sharpen',
},
valueMap: {
fit: {
cover: 'cover',
contain: 'contain',
fill: 'scale-down',
outside: 'crop',
inside: 'pad',
},
gravity: {
auto: 'auto',
side: 'side',
},
},
joinWith: ',',
formatter: (key, value) => encodeQueryItem(key, value),
})
const defaultModifiers = {}
interface CloudflareOptions {
baseURL?: string
}
// https://developers.cloudflare.com/images/image-resizing/url-format/
export default defineProvider<CloudflareOptions>({
getImage: (src, {
modifiers,
baseURL = '/',
}) => {
const mergeModifiers = { ...defaultModifiers, ...modifiers }
const operations = operationsGenerator(mergeModifiers as any)
// https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : src
return {
url,
}
},
})