Skip to content

Dynamically Add and Transform Multiple Image Overlays

Front end developers may want to combine multiple images into a single image. For example, when creating and adding watermarks to stock photos, adding shapes or badges, preparing content for print (e.g. placing a logo on a t-shirt or a mug), adding a caption, and so on.

Multiple images can be combined by overlaying them one on top of the other. However, since it is not a given that both the underlying and overlaying images match each other, let alone your graphic design, you may need to perform further image transformations (e.g. resize, crop, change colors, create a better fit). This is where Cloudinary comes in.

Cloudinary’s image overlay feature helps users easily combine multiple images. It supports image and text overlays using on-the-fly transformation URLs. In this blog post, we will show you how to separately transform, process, and transform underlying and overlaying images, then dynamically generate a resulting image that you can embed on your site.

Suppose you have a website that sells personalized gifts. Users can file upload their own photos, add text, and your site will automatically crop and transform those photos and text on the gift of their choice. For example, a couple may want to place their picture on a coffee mug. This would require you to resize and transform both the underlying image of the coffee mug and the overlaying picture of the couple until they fit together in harmony. Once the images are put in place, you can add text and perform further transformations if necessary.

Below is an example of the original images of the couple and coffee mug that were uploaded to the cloud for further transformation and delivery.

Coffee cup Nice couple

You can add an image overlay using Cloudinary’s overlay parameter (or l for URLs). Returning to our example, here is what the final version of the coffee mug would look like with the overlaying picture of the couple:

Loading code examples Image overlay

Transformation instructions are used to perform the image transformations using dynamic URLs. Cloudinary’s client libraries assist in building these URLs. You can apply multiple chained transformations by separating them with a slash / in your image’s URL.

In order to better transform image overlays, you can set the flags parameter to layer_apply (or fl_layer_apply for URLs), which then tells Cloudinary that all chained transformations that were specified up until the flag, are to be applied on the overlaying image instead of the containing image.

Using our coffee mug example, below you can see how we have applied multiple transformations both on the containing image as well as the overlay. The containing image has been cropped to fill a 400×250 rectangle and the overlaying image of the couple has been cropped using face detection. Color saturation has been increased by 50% and the vignette effect has been applied. Finally, the resulting image has been resized to 100 pixels wide, converted to a circular shape and positioned with 20 pixels offset from the center of the containing image.

Loading code examples Image overlay with further transformation

Learn more about Cloudinary’s image transformation capabilities

In addition to being able to transform a single image overlay, Cloudinary allows you to add and transform multiple overlays, as well. You can do this by chaining another overlay, setting the flags parameter to layer_apply (or fl_layer_apply for URLs), and applying multiple image transformations. Adding another overlay is as simple as transforming a picture to suit the existing underlying and overlaying images. In our coffee mug example, we added a balloon as an additional overlay and performed the following transformations: resized to be 30 pixels wide, changed the hue level to pink, and rotated it five degrees.

Loading code examples Multiple image overlays with transformations

Cloudinary supports adding dynamic text overlays in any style of customized text. What’s more, the text overlay can be further transformed, just like image overlays. So, returning to our example, we have now added a text overlay that we have colored, using the colorize effect, and rotated.

Loading code examples Multiple image and text overlays with further transformation

Cloudinary’s powerful capabilities allow you to transform and generate complex, combined images that match your graphic design requirements. You can use Cloudinary’s dynamic transformation URLs with user-uploaded images in order to transform the images while combining multiple images and text overlays into a single new image. With the new features introduced in this post, you can apply Cloudinary’s rich set of transformation capabilities separately on each layer of underlying or overlaying images or text. All overlay transformation features are available with all Cloudinary plans, including the free tier. If you don’t have a Cloudinary account yet, sign up for a free account here.

Back to top

Featured Post