forked from nuxt/image
-
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.
Showing
11 changed files
with
17,138 additions
and
12,717 deletions.
There are no files selected for viewing
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,22 @@ | ||
--- | ||
title: Imgproxy Provider | ||
description: 'Nuxt Image has first class integration with Imgproxy' | ||
navigation: | ||
title: Imgproxy | ||
--- | ||
|
||
Integration between [Imgproxy](https://imgproxy.net/) and the image module. | ||
|
||
To use this provider you just need to specify the base url of your service in Imgproxy. | ||
|
||
```js{}[nuxt.config.js] | ||
export default { | ||
image: { | ||
imgix: { | ||
baseURL: 'http://imgproxy.example.com', | ||
key: 'xxxxxxxxxxxxxx', | ||
salt: 'xxxxxxxxxxxxxx' | ||
} | ||
} | ||
} | ||
``` |
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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ const BuiltInProviders = [ | |
'glide', | ||
'imagekit', | ||
'imgix', | ||
'imgproxy', | ||
'ipx', | ||
'netlify', | ||
'prismic', | ||
|
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,77 @@ | ||
import { joinURL, withBase } from 'ufo' | ||
import createHmac from 'create-hmac' | ||
import type { ProviderGetImage } from 'src' | ||
import defu from 'defu' | ||
import { createOperationsGenerator } from '~image' | ||
|
||
const operationsGenerator = createOperationsGenerator({ | ||
keyMap: { | ||
resize: 'rs', | ||
size: 's', | ||
fit: 'rt', | ||
width: 'w', | ||
height: 'h', | ||
dpr: 'dpr', | ||
enlarge: 'el', | ||
extend: 'ex', | ||
gravity: 'g', | ||
crop: 'c', | ||
padding: 'pd', | ||
trim: 't', | ||
rotate: 'rot', | ||
quality: 'q', | ||
maxBytes: 'mb', | ||
background: 'bg', | ||
backgroundAlpha: 'bga', | ||
blur: 'bl', | ||
sharpen: 'sh', | ||
watermark: 'wm', | ||
preset: 'pr', | ||
cacheBuster: 'cb', | ||
stripMetadata: 'sm', | ||
stripColorProfile: 'scp', | ||
autoRotate: 'ar', | ||
filename: 'fn', | ||
format: 'f' | ||
}, | ||
formatter: (key, value) => `${key}:${value}` | ||
}) | ||
|
||
function urlSafeBase64 (value: Buffer | string) { | ||
return Buffer.from(value) | ||
.toString('base64') | ||
.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
} | ||
|
||
const hexDecode = (hex: string) => Buffer.from(hex, 'hex') | ||
|
||
function sign (salt: string, target: string, secret: string) { | ||
const hmac = createHmac('sha256', hexDecode(secret)) | ||
|
||
hmac.update(hexDecode(salt)) | ||
hmac.update(target) | ||
|
||
return urlSafeBase64(hmac.digest()) | ||
} | ||
|
||
const defaultModifiers = { | ||
fit: 'fill', | ||
width: 0, | ||
height: 0, | ||
gravity: 'no', | ||
enlarge: 1, | ||
format: 'webp' | ||
} | ||
|
||
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/', key, salt } = {}) => { | ||
const mergeModifiers = defu(modifiers, defaultModifiers) | ||
const encodedUrl = urlSafeBase64(src) | ||
const path = joinURL('/', operationsGenerator(mergeModifiers), encodedUrl) | ||
const signature = sign(salt, path, key) | ||
|
||
return { | ||
url: withBase(joinURL(signature, path), baseURL) | ||
} | ||
} |
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
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
Oops, something went wrong.