You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A [binding](/workers/runtime-apis/bindings/) connects your [Worker](/workers/) to external resources on the Developer Platform, like [Images](/images/transform-images/transform-via-workers/), [R2 buckets](/r2/buckets/), or [KV Namespaces](/kv/concepts/kv-namespaces/).
11
11
@@ -61,26 +61,29 @@ Within your Worker code, you can interact with this binding by using `env.IMAGES
61
61
62
62
For example, to draw a resized watermark on an image:
63
63
64
+
<TypeScriptExample>
65
+
64
66
```ts
65
67
// Fetch the watermark from Workers Assets, R2, KV etc
- You must define [a supported format](/images/transform-images/#supported-output-formats) such as AVIF, WebP, or JPEG for the [transformed image](/images/transform-images/).
@@ -90,6 +93,8 @@ return response;
90
93
91
94
For example, to rotate, resize, and blur an image, then output the image as AVIF:
92
95
96
+
<TypeScriptExample>
97
+
93
98
```ts
94
99
const info =awaitenv.IMAGES.info(stream);
95
100
// Stream contains a valid image, and width/height is available on the info object
@@ -108,6 +113,8 @@ const response = (
108
113
returnresponse;
109
114
```
110
115
116
+
</TypeScriptExample>
117
+
111
118
### `.info()`
112
119
113
120
- Outputs information about the image, such as `format`, `fileSize`, `width`, and `height`.
Copy file name to clipboardExpand all lines: src/content/docs/images/transform-images/draw-overlays.mdx
+51-53Lines changed: 51 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@ pcx_content_type: reference
3
3
title: Draw overlays and watermarks
4
4
sidebar:
5
5
order: 5
6
-
7
6
---
8
7
9
8
You can draw additional images on top of a resized image, with transparency and blending effects. This enables adding of watermarks, logos, signatures, vignettes, and other effects to resized images.
@@ -12,61 +11,59 @@ This feature is available only in [Workers](/images/transform-images/transform-v
12
11
13
12
```js
14
13
fetch(imageURL, {
15
-
cf: {
16
-
image: {
17
-
width:800,
18
-
height:600,
19
-
draw: [
20
-
{
21
-
url:'https://example.com/branding/logo.png', // draw this image
22
-
bottom:5, // 5 pixels from the bottom edge
23
-
right:5, // 5 pixels from the right edge
24
-
fit:'contain', // make it fit within 100x50 area
25
-
width:100,
26
-
height:50,
27
-
opacity:0.8, // 20% transparent
28
-
},
29
-
],
30
-
},
31
-
},
14
+
cf: {
15
+
image: {
16
+
width:800,
17
+
height:600,
18
+
draw: [
19
+
{
20
+
url:"https://example.com/branding/logo.png", // draw this image
21
+
bottom:5, // 5 pixels from the bottom edge
22
+
right:5, // 5 pixels from the right edge
23
+
fit:"contain", // make it fit within 100x50 area
24
+
width:100,
25
+
height:50,
26
+
opacity:0.8, // 20% transparent
27
+
},
28
+
],
29
+
},
30
+
},
32
31
});
33
32
```
34
33
35
34
## Draw options
36
35
37
36
The `draw` property is an array. Overlays are drawn in the order they appear in the array (the last array entry is the topmost layer). Each item in the `draw` array is an object, which can have the following properties:
38
37
38
+
-`url`
39
+
- Absolute URL of the image file to use for the drawing. It can be any of the supported file formats. For drawing watermarks or non-rectangular overlays, Cloudflare recommends that you use PNG or WebP images.
39
40
41
+
-`width` and `height`
42
+
- Maximum size of the overlay image, in pixels. It must be an integer.
40
43
41
-
*`url`
42
-
* Absolute URL of the image file to use for the drawing. It can be any of the supported file formats. For drawing watermarks or non-rectangular overlays, Cloudflare recommends that you use PNG or WebP images.
43
-
44
-
*`width` and `height`
45
-
* Maximum size of the overlay image, in pixels. It must be an integer.
44
+
-`fit` and `gravity`
45
+
- Affects interpretation of `width` and `height`. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
46
46
47
-
*`fit` and `gravity`
48
-
* Affects interpretation of `width`and `height`. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
47
+
-`opacity`
48
+
- Floating-point number between `0` (transparent) and `1` (opaque). For example, `opacity: 0.5` makes overlay semitransparent.
49
49
50
-
*`opacity`
51
-
* Floating-point number between `0` (transparent) and `1` (opaque). For example, `opacity: 0.5` makes overlay semitransparent.
50
+
-`repeat`
51
+
- If set to `true`, the overlay image will be tiled to cover the entire area. This is useful for stock-photo-like watermarks.
52
+
- If set to `"x"`, the overlay image will be tiled horizontally only (form a line).
53
+
- If set to `"y"`, the overlay image will be tiled vertically only (form a line).
52
54
53
-
*`repeat`
54
-
* If set to `true`, the overlay image will be tiled to cover the entire area. This is useful for stock-photo-like watermarks.
55
-
* If set to `"x"`, the overlay image will be tiled horizontally only (form a line).
56
-
* If set to `"y"`, the overlay image will be tiled vertically only (form a line).
57
-
58
-
*`top`, `left`, `bottom`, `right`
59
-
* Position of the overlay image relative to a given edge. Each property is an offset in pixels. `0` aligns exactly to the edge. For example, `left: 10` positions left side of the overlay 10 pixels from the left edge of the image it is drawn over. `bottom: 0` aligns bottom of the overlay with bottom of the background image.
55
+
-`top`, `left`, `bottom`, `right`
56
+
- Position of the overlay image relative to a given edge. Each property is an offset in pixels. `0` aligns exactly to the edge. For example, `left: 10` positions left side of the overlay 10 pixels from the left edge of the image it is drawn over. `bottom: 0` aligns bottom of the overlay with bottom of the background image.
60
57
61
58
Setting both `left` and `right`, or both `top` and `bottom` is an error.
62
59
63
60
If no position is specified, the image will be centered.
64
61
65
-
*`background`
66
-
* Background color to add underneath the overlay image. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
62
+
-`background`
63
+
- Background color to add underneath the overlay image. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
67
64
68
-
*`rotate`
69
-
* Number of degrees to rotate the overlay image by. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
65
+
-`rotate`
66
+
- Number of degrees to rotate the overlay image by. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
70
67
71
68
## Draw using the Images binding
72
69
@@ -76,14 +73,15 @@ The accepted options for the overlaid image are `opacity`, `repeat`, `top`, `lef
0 commit comments