Last updated: Jul-02-2023
You can add arithmetic and/or user-defined variables to your transformations. These expressions are evaluated on the fly, enabling you to add an additional layer of sophistication and flexibility to your transformations.
- Arithmetic expressions enable you to assign values to a parameter or variable based on an arithmetic equation.
- User-defined variables enable you to keep value assignment separate from the transformation definition.
User-defined variables are especially valuable when used within named transformations. This enables complete separation of the transformation from the varying values used for delivery. This also makes it significantly easier to reuse common transformations for many assets, even when some specific adjustments must be made to the transformation depending on the specific asset or other data passed from another source.
You can achieve many complex transformation goals by using user-defined variables in conjunction with arithmetic expressions and conditional transformations.
You can create arithmetic expressions by using arithmetic operators with numeric transformation parameters or user-defined variables.
For example, you could set a relative shadow size for an image (the
y of the e_shadow parameter) by setting these parameters to be equal to 2% of the current width of that image (
iw_mul_2_add_ih_mul_2 would give you the perimeter measurement of the initial asset by first multiplying both the initial width by 2 and the initial height by 2, and then adding these two products.
|operation||URL syntax||SDK syntax|
(to the power of)
To use user-defined variables in your transformations, you first declare and assign values to the variables you want to use. In later components of your chained transformation, you can use the variables as the values of your transformation parameters.
User-defined variables start with the
$ sign, for example
$newwidth. The name can include only alphanumeric characters and must begin with a letter.
Variables can be assigned a number value, string value, or they can take on the value of a numeric asset characteristic, such as
iw (initial width) or
fc (face count).
When working with string values:
- String values are bounded by
- To assign the value of an existing contextual metadata key to a variable, use the syntax:
ctx:!key_name!. When you set a transformation parameter to that variable, it gets the value of the specified contextual metdata key.
- To assign the value of a structured metadata ID to a variable, use the syntax:
md:!external_id!. Be sure to use the external ID, not the label. When you set a transformation parameter to that variable, it gets the value of the specified metadata.
You can provide several values in a string using a colon
:as the delimiter. For example:
!string1:string2:string3!. These multiple-value strings can be used:
- as separators for public IDs with slashes when the variable will be used to control the public ID of an overlay.
- for comparing multiple values when the variable will be used in conjunction with the
_nin_conditional transformation comparison operators.
- to specify RGB Hex values for the
colorparameters. For example:
When a variable that is defined as a string value and contains a colon, is passed to any other parameter, the colon is treated as a literal character.
Use the underscore to assign a value to a variable. For example:
To convert a string value to a numeric value, append
_to_i for an integer, or
_to_f for a float. For example, to assign the value of the contextual metadata key,
breadth, to the variable
- as an integer, use the syntax:
- as a float, use the syntax:
If assigning structured metadata to a variable, and the metadata type is numeric, you do not need to specify
_to_i. For example, if you have a number field with external ID,
position, you can use the syntax:
$newwidth_md:!position!. However, if it is a text field you still need to append
_to_f to convert it to an integer or float.
You can use arithmetic operators with numeric variables just as you do with numeric transformation parameters, or even in combination. Consider this example:
$smallvariable is set to 150
$bigvariable is assigned the value of 2 times the
heightparameter is set to use the value of
widthparameter is set to use the value of
User-defined variables for images support the following variable types and parameters:
You can apply a numeric variable value to the following transformation parameters:
e (effect): for the numeric strength value of an effect. For example:
bo (border): for the numeric pixel width of the border. For example:
Note that even though the syntax for directly setting the border width is usually a string element (e.g.
7px), when you want to pass the value as a variable, you pass it as a number value and not a string.
Use-case example: Set width and aspect ratio using variables
You can apply a string variable for the following:
- Text overlays
- Image overlays
- Multiple string value comparisons
- Border and background color
- Self-referencing variables
You can use a variable for the name and/or any value of an effect. For example:
You can use a variable for all or part of the text value in a text overlay.
To mix static text with a string variable when specifying a text overlay, use the syntax:
static text $(variable) more static text. For example:
$name_!Max!/l_text:arial_10:My name is $(name) the Magnificent/fl_layer_apply
You can also use variables for the text style and color. For example:
Use-case example: Text overlay with string variable
You can use a variable for the public ID of an image overlay. For example:
- If the public ID for the overlay includes slashes, replace the slashes with colons. For example, the same transformation from above, but overlaying 'mypath/myimage':
- To use a variable with authenticated overlays, include the authenticated string as part of the variable as well, for example:
Use-case example: Image overlay variable in a named transformation
You can use a variable to set the color of a
bo in URLs) or
b in URLs) to either a color name or an RGB Hex value. For RGB hex values, use the syntax
For example, the following transformation sets the variable
$bcolor to a specified rgb shade of blue. Afterwards, a condition checks whether the image has at least one face (
face_count > 0), and if so, it changes the
$bcolor variable value to red. Lastly, the image is given a border whose color is determined by the
Using the identical transformation on an image with at least one face applies a red border:
You can use a variable when verifying that all of the specified values are (or are not) contained within another set of values. For example:
mdparameters, their values are exposed publicly in the URL. If you want to prevent such values from being exposed, you can disable the Usage of tags/context/metadata in transformation URLs option in the Security Settings (enabled by default). When this setting is disabled, any URL that exposes tags, contextual metadata or structured metadata values will return an error.
You can send additional files to a Custom function by passing them as a base64 encoded string using reference variables. These variables are passed to WebAssembly and remote functions through a JSON metadata structure together with contextual metadata and tags, as explained in the Custom functions documentation.
To define a reference variable, insert
ref: before the value of the variable. For example:
In the above example,
myfile can be the public ID of any file stored in Cloudinary or a URI. If the public ID includes slashes, replace the slashes with colons. For example, to reference 'path1/path2/myfile', use:
Use-case example: Pass a PNG file to a WebAssembly function to use as an overlay
You can set a user-defined variable to
current to refer to the image being delivered in the current URL. This enables you to use the image being delivered as the value for other transformations without needing to know the image's public_id when you are building the transformation. This also enables you to reference the delivered image from within a named transformation.
There are two ways to use the
pixel buffer: The default
currentrepresents the actual current pixel buffer. This means that if you apply transformations to the delivered image and then assign
currentto a variable,
currenttakes on the transformed pixel buffer at that point. If in the example above, we had first applied the
e_grayscaletransformation, and in the next component of the chain, we defined
$img_current, and following that applied the
$imgvariable as an overlay, the overlay would get the transformed grayscale image.
public_id: You can alternatively assign a variable to
current:public_id. This captures the
unique identifierof the original asset (type, resource type and public ID) as a string. You can pass this value to an image overlay or underlay parameter if you want to reference the original image, regardless of where it is defined in the transformation chain.
Use-case example: Use a self-referencing variable to create a blurred padding background
Within a transformation component, condition (if) statements are evaluated first, then variable assignments, then transformations. Therefore:
When an assignment is made in a conditional component, the assignment is evaluated only if the condition is true.
Even if a transformation is specified before a variable assignment within the same URL component, the value is assigned before the transformation. For example, if you deliver a URL containing the following condition:
Even though the variable
xis assigned the value
5only at the end of the transformation, that assignment will be applied first. Afterwards, the
widthtransformation takes on the value (5) of the
However, the best practice is to separate conditions from results in separate URL components in the order you want them evaluated. For example:
if_w_gt_5/$x_5/c_scale,w_$x/if_end, as explained in Conditional transformations.
Use-case example: Set height based on aspect ratio
Set the variable
200, and the
$ar parameter to
0.8. Then set aspect ratio parameter to the
$ar value and the width parameter to the
$w value, along with
Create a new variable called
$newwidth. Set the value of the variable to be the image's initial width multiplied by
0.3 (according to that standard order of operations), and then add
10. Resize the image by setting the width parameter to the
Check whether the image has portrait orientation (aspect ratio < 1). If so, set the
$height variable to
300. Otherwise set the
$height variable to
200. Then set the height of the delivered image to be the assigned
Set the variable
$award to the string value
!First!. Then set the text overlay to use the text
$(award) Place. The style and color of the text are also set using variables (
$color). The text overlay is placed near the bottom of the image on a semi-transparent, rounded border.
In a named transformation called
passport_photo, define the cropping for an image to be a 70% zoomed thumbnail with face detection, using a variable for the width and height, where height is relative to width, to get the legal 3.5x4.5 (0.78) ratio. There is also a white border with a gray edge that emulates a printed photo border.
Named transformation definition:
In the delivery URL code, you define the value for the width parameter, which is applied to the named transformation to yield a printable passport photo of the desired size:
The 3 images above use essentially identical delivery URLs, each calling the same named transformation, only with different values assigned to the
$width variable: 115, 140, or 165.
Create a named transformation called
image_ratings with the following definition:
The image overlay is defined as a variable called
rating. You can then assign the variable value with either the thumbs-up or thumbs-down image to display the rating for a particular image. For example:
Pass the asset with public ID:
docs/sale.png, to the WebAssembly function with public ID:
docs/pnglayer.wasm, as a reference variable called
overlay. The PNG file is overlaid on the
This is the generated URL:
Here is the Rust code that was compiled to
pnglayer.wasm. Notice how the variable called
overlay is obtained in the code
Check whether the image width is less than 800 pixels. If so, add a blurred, increased-brightness version of the current image behind the delivered image. The blurred image will be 800 pixels wide and the same height as the original image.
Try the Center objects using trim cookbook recipe for a step-by-step walk through of trimming solid background from an image and then centering it on a background whose size is determined by variables based on the images initial width and height.