Skip to content

Draw overlays and watermarks

Use Images to draw text, watermarks, and logos over other images. Overlays support transparency, positioning, and compositing modes.

You can draw overlays through all three Images interfaces:

Draw with the URL interface or cf.image in Workers

Use the draw parameter in a transformation URL or the draw array in cf.image on a Worker fetch() subrequest. Overlay images and custom fonts must be accessible through a URL.

Each entry draws either an image or text:

  • Image — Set url to the absolute URL of the image. Apply optimization parameters like width, height, fit, blur, and rotate.
  • Text — Set text to the string to render. Style it with the same font, color, and size settings that are available in the binding .text() method. Refer to .text() for the available options, defaults, and limits.

Overlays are drawn in the order they appear — the last entry is the topmost layer.

https://example.com/cdn-cgi/image/width=800,height=600,draw=((url='/branding/logo.png',bottom=5,right=5,fit=contain,width=100,height=50,opacity=0.8),(text=HELLO,font=(url='/font.otf'),color=rgb(0,0,0),size=96,top=40))/image.png
export default {
  async fetch() {
    const imageURL = "https://example.com/image.png";

    return fetch(imageURL, {
      cf: {
        image: {
          width: 800,
          height: 600,
          draw: [
            {
              url: "https://example.com/branding/logo.png",
              bottom: 5,
              right: 5,
              fit: "contain",
              width: 100,
              height: 50,
              opacity: 0.8,
            },
            {
              text: "HELLO",
              font: { url: "https://example.com/font.otf" },
              color: "#223E49",
              size: 96,
              top: 40,
            },
          ],
        },
      },
    });
  },
};

Draw with the Images binding

The Images binding uses a chainable .draw() method to draw an overlay on top of an image. You can chain multiple .draw() calls for multiple overlays.

Pass the overlay as the first argument, then the draw settings as the second. The overlay can be an image or text:

  • Image — Pass the image bytes or an .input() chain. To apply optimization parameters to the overlay image, pass an .input() chain with .transform() as the first argument.
  • Text — Pass a handle created with .text(). Set the font, color, and size in the .text() options argument. Refer to .text() for the available options, defaults, and limits.
export default {
	async fetch(request, env) {
		const img = await fetch("https://example.com/base.png");
		const watermark = await fetch("https://example.com/overlay.png");

		const response = (
			await env.IMAGES.input(img.body)
				.draw(env.IMAGES.input(watermark.body).transform({ width: 100 }), {
					bottom: 10,
					right: 10,
					opacity: 0.5,
				})
				.draw(
					env.IMAGES.text("HELLO", {
						font: { url: "https://example.com/font.otf" },
						color: "#223E49",
						size: 96,
					}),
					{ top: 40 },
				)
				.output({ format: "image/avif" })
		).response();

		return response;
	},
};

Settings

The dimensions of the output image are always determined by the base image. The overlay is then drawn onto the base image's canvas.

The following draw-specific settings can be used for positioning and blending. In the URL interface and cf.image, these settings are grouped beneath draw. With the Images binding, pass the corresponding settings as the second argument to .draw().

url

Absolute URL of the overlay image when drawing with the URL interface or with cf.image. url applies only to image overlays. To draw text, create the overlay using text instead.

Accepts any supported image format. For watermarks or non-rectangular overlays, use PNG or WebP images.

When drawing an image overlay with the binding, the overlay is passed as image bytes or an .input() chain instead of a URL. Refer to Draw with the Images binding.

text

Renders a text string as an overlay when drawing with the URL interface or with cf.image. When drawing with the Images binding, create the text overlay with .text() instead.

Style the text with font, color, and size settings. These apply to the URL interface, cf.image text entries, and the binding .text() method. Refer to .text() for the available settings, defaults, and limits.

width and height

Sets the maximum dimensions of the overlay image when drawing with the URL interface or with cf.image. These settings apply only to image overlays. The dimensions of a text overlay are determined by its text string and styling.

Accepts an integer pixel count or a percentage string such as 50p. For example, height: "25p" (cf.image) sets the overlay height to 25% of the height of the base image.

Use fit and gravity to control how the overlay image is resized and cropped.

When drawing with the Images binding, the dimensions of the overlay image can be set using the .transform() method.

repeat

Determines whether to tile the overlay across the image.

Accepts the following values:

  • true — Tiles the overlay to cover the entire area. This is useful for watermarks.
  • x — Tiles the overlay horizontally only.
  • y — Tiles the overlay vertically only.

top, left, bottom, right

Sets the position of the overlay as an offset, in pixels, to the specified edge. 0 aligns the overlay flush to the edge. If no position is specified, then the overlay is centered.

For example, { bottom: 0, right: 10 } (cf.image) places the overlay at the bottom-right corner, 10 pixels inward from the right edge.

Setting both left and right, or both top and bottom returns an error.

opacity

Sets the opacity of the overlay. Accepts a decimal value between 0.0 (fully transparent) and 1.0 (fully opaque). For example, opacity: 0.5 (cf.image) makes the overlay semitransparent.

composite

Controls how the overlay is blended with the base image using Porter-Duff compositing operations ↗︎. The default is over.

The composite mode only affects the area within the overlay's bounding box. The base image is always preserved outside of this area.

Accepts the following values:

over

Draws the overlay on top of the base image. This is the default composite behavior.

The overlay covers the base where they overlap. Both images are visible where they do not overlap.

composite=over output

in

Shows the overlay only where the base image is opaque. If the overlay has transparent pixels that overlap with the base image, then those areas of the base image will also become transparent.

composite=in output

atop

Draws the overlay on top of the base image, but only where the base image is opaque. This will clip the overlay to the shape of the base image.

If the overlay has transparent pixels that overlap with the base image, then the base image remains visible (unlike in).

composite=atop output

out

Shows the overlay only where the base image is transparent. Within the overlay's bounding box, opaque areas of the base image will become transparent.

composite=out output

xor

Shows the areas of each image where the other is transparent. Overlapping opaque areas will become transparent. This can be used to create rounded corners or custom shapes.

composite=xor output

lighter

Adds the color values of both images, which makes the overlapping areas brighter.

composite=lighter output

Examples

Watermark

Tile a semitransparent watermark across the entire image.

https://example.com/cdn-cgi/image/draw=((url=/watermark.png,repeat=true,opacity=0.2))/image.png
fetch(imageURL, {
  cf: {
    image: {
      draw: [
        {
          url: "https://example.com/watermark.png",
          repeat: true,
          opacity: 0.2,
        },
      ],
    },
  },
});

Logo in the corner

Position a logo at the bottom-right corner.

https://example.com/cdn-cgi/image/draw=((url=/logo.png,bottom=5,right=5))/image.png
fetch(imageURL, {
  cf: {
    image: {
      draw: [
        {
          url: "https://example.com/logo.png",
          bottom: 5,
          right: 5,
        },
      ],
    },
  },
});

Multiple overlays

Combine multiple overlays in one request. They are drawn in order — the last entry is the topmost layer.

https://example.com/cdn-cgi/image/draw=((url=/watermark.png,repeat=true,opacity=0.2),(url=/play-button.png),(url=/logo.png,bottom=5,right=5))/image.png
fetch(imageURL, {
  cf: {
    image: {
      draw: [
        {
          url: "https://example.com/watermark.png",
          repeat: true,
          opacity: 0.2,
        },
        { url: "https://example.com/play-button.png" },
        { url: "https://example.com/logo.png", bottom: 5, right: 5 },
      ],
    },
  },
});

Rounded corners

Cut rounded corners from an image. A corner mask is drawn at each corner and rotated to match the position. xor removes the overlapping pixels.

The example below shows how this can be done using the Images binding.

const image = await fetch("https://example.com/photo.png");
const mask = await fetch("https://example.com/corner-mask.png");

let [topLeft, topRight] = mask.body.tee();
let bottomLeft, bottomRight;
[topLeft, bottomLeft] = topLeft.tee();
[topLeft, bottomRight] = topLeft.tee();

const output = await env.IMAGES.input(image.body)
	.draw(env.IMAGES.input(topLeft).transform({ rotate: 0 }), {
		left: 0,
		top: 0,
		composite: "xor",
	})
	.draw(env.IMAGES.input(topRight).transform({ rotate: 90 }), {
		right: 0,
		top: 0,
		composite: "xor",
	})
	.draw(env.IMAGES.input(bottomRight).transform({ rotate: 180 }), {
		bottom: 0,
		right: 0,
		composite: "xor",
	})
	.draw(env.IMAGES.input(bottomLeft).transform({ rotate: 270 }), {
		bottom: 0,
		left: 0,
		composite: "xor",
	})
	.output({ format: "image/png" });

return output.response();

Was this helpful?