-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ImageEngine provider (#456)
- Loading branch information
Showing
8 changed files
with
193 additions
and
2 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,82 @@ | ||
--- | ||
title: ImageEngine Provider | ||
description: 'Nuxt Image has first class integration with ImageEngine' | ||
navigation: | ||
title: ImageEngine | ||
--- | ||
|
||
Integration between [ImageEngine](https://imageengine.io/) and the image module. | ||
|
||
At a minimum, you must configure the `imageengine` provider with the `baseURL` set to your ImageEngine Delivery Address: | ||
|
||
```js{}[nuxt.config.js] | ||
export default { | ||
image: { | ||
imageengine: { | ||
baseURL: 'https://xxxxx.imgeng.in' | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## ImageEngine `fit` values | ||
|
||
The [standard values for `fit` property](/components/nuxt-img#fit) map onto [ImageEngine Directives](https://imageengine.io/docs/directives) as follows: | ||
|
||
* `cover`: `m_cropbox` | ||
* `contain`: `m_letterbox` | ||
* `fill`: `m_stretch` | ||
* `inside`: `m_box`, this is the default fit method for the ImageEngine provider. | ||
* `outside`: This fit method is not supported and functions the same as `inside`. | ||
|
||
## ImageEngine modifiers | ||
|
||
In addition to the [standard modifiers](/components/nuxt-img#modifiers), you can also use all [ImageEngine Directives](https://imageengine.io/docs/directives) by adding them to the `modifiers` property with the following attribute names: | ||
|
||
* `format`: `f` directive | ||
* `fit`: `m` directive | ||
* `passThrough`: `pass` directive | ||
* `sharpen`: `s` directive | ||
* `rotate`: `r` directive | ||
* `screenPercent`: `pc` directive | ||
* `crop`: `cr` directive | ||
* `inline`: `in` directive | ||
* `metadata`: `meta` directive | ||
|
||
> Note that the standard `quality` attribute is converted to the ImageEngine `cmpr` compression directive. `quality` is the opposite of compression, so 80% quality equals 20% compression. Since ImageEngine automatically adapts image quality the visitor's device, browser and network quality, it is recommended that you do not explicitly set the quality. If you want to completely disable all optimizations to an image, you should use `:modifiers="{ passThrough: 'true' }"`, which will serve the unaltered image. | ||
Example 1: Cropping an image to a width and height of 100x80, starting 10 pixels from the top and 20 pixels from the left: | ||
|
||
```html | ||
<nuxt-img | ||
provider="imageengine" | ||
src="/some-image.jpg" | ||
width="100" | ||
height="80" | ||
:modifiers="{ cr: '100,80,10,20' }" | ||
/> | ||
``` | ||
|
||
Example 2: Instruct ImageEngine to keep the image's EXIF metadata (which is normally removed to reduce the image byte size): | ||
|
||
```html | ||
<nuxt-img | ||
provider="imageengine" | ||
src="/some-image.jpg" | ||
width="100" | ||
height="80" | ||
:modifiers="{ meta: 'true' }" | ||
/> | ||
``` | ||
|
||
## ImageEngine best practices | ||
|
||
### Automatic format and quality detection | ||
|
||
ImageEngine automatically detects the best format and quality for your image based on the characteristics of the specific device that requested it. | ||
|
||
This feature is very effective and it is not recommended that you disable it, but if you must, you can manually specify the format (ex: `format='webp'`) and quality (`quality='80'`). | ||
|
||
### ImageEngine settings | ||
|
||
ImageEngine allows you to set all of the modifiers/directives in the ImageEngine control panel under advanced settings. This is a good place to set default adjustments since you can modify them without having to make changes to your Nuxt codebase. If a directive is set in both Nuxt and the ImageEngine User-Adjustable Settings, the value in Nuxt takes 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
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,53 @@ | ||
import { joinURL } from 'ufo' | ||
import type { ProviderGetImage } from 'src' | ||
import { createOperationsGenerator } from '~image' | ||
|
||
export const operationsGenerator = createOperationsGenerator({ | ||
keyMap: { | ||
width: 'w', | ||
height: 'h', | ||
quality: 'cmpr', | ||
format: 'f', | ||
fit: 'm', | ||
passThrough: 'pass', | ||
sharpen: 's', | ||
rotate: 'r', | ||
screenPercent: 'pc', | ||
crop: 'cr', | ||
inline: 'in', | ||
metadata: 'meta' | ||
}, | ||
valueMap: { | ||
fit: { | ||
cover: 'cropbox', | ||
contain: 'letterbox', | ||
fill: 'stretch', | ||
inside: 'box', | ||
outside: 'box' | ||
}, | ||
format: { | ||
jpeg: 'jpg' | ||
}, | ||
quality (value: string) { | ||
// ImageEngine uses compression, which is the opposite of quality, | ||
// so quality 90 == compression 10. Convert using: compression = 100 - quality | ||
let compression = (100 - parseInt(value, 10)) | ||
|
||
// ImageEngine's values are 0-99 (100 values), whereas Nuxt uses 0-100 (101 values) | ||
// so we clip the upper bound at 99 if 100 was requested. | ||
if (compression === 100) { | ||
compression = 99 | ||
} | ||
return compression.toString() | ||
} | ||
}, | ||
joinWith: '/', | ||
formatter: (key, value) => `${key}_${value}` | ||
}) | ||
|
||
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/' } = {}) => { | ||
const operations = operationsGenerator(modifiers) | ||
return { | ||
url: joinURL(baseURL, src + (operations ? ('?imgeng=/' + operations) : '')) | ||
} | ||
} |
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