Skip to content

Commit 9588c33

Browse files
deven96ToriLindsaydochne
authored andcommitted
Adding face cropping parameters to image transformation (#23592)
* Adding face cropping parameters to image transformation * Fixing face cropping documentation * Merge updates from upstream * Adding responsible AI to face cropping * Update src/content/partials/images/gravity.mdx * Update src/content/docs/images/transform-images/transform-via-workers.mdx Co-authored-by: ToriLindsay <[email protected]> --------- Co-authored-by: ToriLindsay <[email protected]> Co-authored-by: Doug <[email protected]>
1 parent 8237917 commit 9588c33

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

src/content/docs/images/transform-images/transform-via-url.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ You must specify at least one option. Options are comma-separated (spaces are no
135135

136136
<Render file="width" />
137137

138+
### `zoom`
139+
140+
<Render file="zoom" />
141+
138142
## Recommended image sizes
139143

140144
Ideally, image sizes should match exactly the size they are displayed on the page. If the page contains thumbnails with markup such as `<img width="200" …>`, then images should be resized to `width=200`. If the exact size is not known ahead of time, use the [responsive images technique](/images/manage-images/create-variants/).

src/content/docs/images/transform-images/transform-via-workers.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ The `fetch()` function accepts parameters in the second argument inside the `{cf
118118

119119
<Render file="width" />
120120

121+
### `zoom`
122+
123+
<Render file="zoom" />
124+
121125
In your worker, where you would fetch the image using `fetch(request)`, add options like in the following example:
122126

123127
```js
@@ -281,4 +285,4 @@ When testing image resizing, please deploy the script first. Resizing will not b
281285

282286
Resized images are always cached. They are cached as additional variants under a cache entry for the URL of the full-size source image in the `fetch` subrequest. Do not worry about using many different Workers or many external URLs — they do not influence caching of resized images, and you do not need to do anything for resized images to be cached correctly.
283287

284-
If you use the `cacheKey` fetch option to unify caches of multiple different source URLs, you must not add any resizing options to the `cacheKey`, as this will fragment the cache and hurt caching performance. The `cacheKey` option is meant for the full-size source image URL only, not for its resized variants.
288+
If you use the `cacheKey` fetch option to unify the caches of multiple source URLs, do not include any resizing options in the `cacheKey`. Doing so will fragment the cache and hurt caching performance. The `cacheKey` should reference only the full-size source image URL, not any of its resized versions.

src/content/partials/images/gravity.mdx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import { Tabs, TabItem } from "~/components";
66

7-
When cropping with `fit: "cover"` and `fit: "crop"`, this parameter defines the side or point that should not be cropped. Available options are:
7+
Specifies how an image should be cropped when used with `fit=cover` and `fit=crop`.
8+
Available options are `auto`, `face`, a side (`left`, `right`, `top`, `bottom`), and relative coordinates (`XxY` with a valid range of `0.0` to `1.0`):
89

910
- `auto`\
1011
Selects focal point based on saliency detection (using maximum symmetric surround algorithm).
@@ -17,6 +18,13 @@ When cropping with `fit: "cover"` and `fit: "crop"`, this parameter defines the
1718
You must subtract the height of the image before you calculate the focal point.
1819
:::
1920

21+
- `face`\
22+
Automatically sets the focal point based on detected faces in an image. This can be combined with the `zoom` parameter to specify how closely the image should be cropped towards the faces.
23+
24+
The new focal point is determined by a minimum bounding box that surrounds all detected faces. If no faces are found, then the focal point will fall back to the center of the image.
25+
26+
This feature uses an open-source model called RetinaFace through WorkersAI. Our model pipeline is limited only to facial detection, or identifying the pixels that represent a human face. We do not support facial identification or recognition. Read more about Cloudflare's [approach to responsible AI](https://www.cloudflare.com/trust-hub/responsible-ai/).
27+
2028
<Tabs>
2129
<TabItem label="URL format">
2230
```js
@@ -29,6 +37,10 @@ You must subtract the height of the image before you calculate the focal point.
2937
OR
3038

3139
gravity=0x1
40+
41+
OR
42+
43+
gravity=face
3244
```
3345

3446
</TabItem>
@@ -43,6 +55,10 @@ You must subtract the height of the image before you calculate the focal point.
4355
OR
4456

4557
g=0x1
58+
59+
OR
60+
61+
g=face
4662
```
4763

4864
</TabItem>
@@ -58,7 +74,10 @@ OR
5874

5975
cf: {image: {gravity: {x:0.5, y:0.2}}}
6076

77+
OR
78+
79+
cf: {image: {gravity: "face"}}
6180
```
6281
</TabItem>
6382
</Tabs>
64-
```
83+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
{}
3+
---
4+
import { Tabs, TabItem } from "~/components"
5+
6+
Specifies how closely the image is cropped toward the face when combined with the `gravity=face` option.
7+
Valid range is from `0` (includes as much of the background as possible) to `1` (crops the image as closely to the face as possible), decimals allowed. The default is `0`.
8+
9+
This controls the threshold for how much of the surrounding pixels around the face will be included in the image and takes effect only if face(s) are detected in the image.
10+
11+
<Tabs>
12+
<TabItem label="URL format">
13+
```js
14+
zoom=0.1
15+
```
16+
</TabItem>
17+
<TabItem label="URL format alias">
18+
```js
19+
zoom=0.2
20+
OR
21+
22+
face-zoom=0.2
23+
```
24+
</TabItem>
25+
<TabItem label="Workers">
26+
```js
27+
cf: {image: {zoom: 0.5}}
28+
```
29+
</TabItem>
30+
</Tabs>

0 commit comments

Comments
 (0)