Self-referencing variables in transformations
There are lots of times when you might want to overlay (or underlay) a different transformation of the same image.
For example, you might want to have a small, cropped and zoomed version of a product as a layer over a full sized image of a product, or you might want a blurred landscape version of a picture as the background (underlay) for a portrait photo.
Did you know that you can use self-referencing variables in your transformations to accomplish this at scale for any image? In other words, you can set your transformation to automatically use whatever the base image's public ID is also for your overlay or underlay image.
To do this, just set a user-defined variable (such as $img
) to the value current
and then pass that user-defined variable instead of a public_id to your overlay or underlay.
Here are a couple nice examples:
(This one also uses a conditional transformation, so that the self-referencing underlay will only get added in case the base image's width is less than than the overall image width you want to deliver)
Since these transformations are using variables, you can save these transformations as named transformations and then use them on any image in your account!
---
There are so many applications for self-referencing variables. Have you ever implemented one? Or did this post get you trying out some ideas?
We'd love to see some more cool implementations of self-referencing variables. Please share yours below for your fellow developers to enjoy!