A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloudinary.com/documentation/user_defined_variables below:

Transform Images with Variables & Arithmetic Operators

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.

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.

See also: Variable and arithmetic video transformations.

Arithmetic expressions

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 x and y of the e_shadow parameter) by setting these parameters to be equal to 2% of the current width of that image (w_div_50):

When you use multiple arithmetic operators in an expression, standard order of operations apply (multiplication and division before addition and subtraction).

For example, 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.

Supported arithmetic operators operation URL syntax SDK syntax add add + subtract sub - multiply mul * divide div / modulo (remainder) mod % pow
(to the power of) pow ^ User-defined variables - overview

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.

Variable naming

User-defined variables start with the $ sign, for example $newwidth. The name can include only alphanumeric characters and must begin with a letter.

Variable value types

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:

Assigning values to variables

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 $newwidth:

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_i or _to_f to convert it to an integer or float.

Using arithmetic expressions with variables

You can use arithmetic operators with numeric variables just as you do with numeric transformation parameters, or even in combination. Consider this example:

$small_150/$big_2_mul_$small/c_fill,h_$small_add_20,w_$big

Testing whether a variable has been defined

You can test whether or not a variable has been defined using the parameters if_isdef_$<variable name> and if_isndef_$<variable name>.

For example, check if the $big variable has been defined, and if so, set the width:

if_isdef_$big/c_scale,w_$big/if_end

Similarly, if the $small variable is not defined, set it to a default value:

if_isndef_$small/$small_300/if_end

Learn more about conditional transformations.

Variable types and supported parameters

User-defined variables for images support the following variable types and parameters:

Numeric variables

You can apply a numeric variable value to the following transformation parameters:

Use-case example: Set width and aspect ratio using variables

String variables

You can apply a string variable for the following:

Effects

You can use a variable for the name and/or any value of an effect. For example:

Some effects allow you to specify arrays of strings, for example, the

prompt

parameter of the

gen_recolor

effect. Variables cannot currently be used in arrays.


Text overlays

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:

You can also use variables for the text style and color. For example:

Use-case example: Text overlay with string variable

Image overlays

You can use a variable for the public ID of an image overlay. For example:

$overlay_!sample!/c_fill,h_400,w_400/l_$overlay/c_scale,w_100/fl_layer_apply,g_south_east,x_20,y_20

Use-case example: Image overlay variable in a named transformation

Border and background color

You can use a variable to set the color of a border (bo in URLs) or background (b in URLs) to either a color name or an RGB Hex value. For RGB hex values, use the syntax $var_!rgb:######!.

For example, the following transformation sets the variable $bcolor to red. 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 a specific shade of blue (#374663). Lastly, the image is given a border whose color is determined by the $bcolor variable.

 

Using the identical transformation on an image with at least one face applies the defined blue color for the border:

Multiple string value comparisons

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:

$mystringset_!string1:string2:string3!/if_$mystringset_in_tags,....

When you use variable or conditional expressions that include the

tags

,

ctx

or

md

parameters, 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.

File reference variables

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:

$file_ref:!myfile!/fn_wasm:my_example.wasm

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:

$file_ref:!path1:path2:myfile!/fn_wasm:my_example.wasm

Use-case example: Pass a PNG file to a WebAssembly function to use as an overlay

Self-referencing variables

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.

For example: $img_current/e_grayscale/l_$img/bo_30px_solid_white/c_scale,w_0.3/fl_layer_apply,g_north_west

This will cause the delivered grayscale image to also be used as a color overlay, in this case, displayed at the top left with a white border:

There are two ways to use the current variable.

Use-case example: Use a self-referencing variable to create a blurred padding background

Expression evaluation order

Within a transformation component, condition (if) statements are evaluated first, then variable assignments, then transformations. Therefore:

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

Use-case examples

For purposes of simplicity, most of these examples do not use named transformations. However, in reality, you will often get the most value from variables when used with named transformations.

Simple

Set the variable $w to 200, and the $ar parameter to 0.56 (9:16). Then set aspect ratio parameter to the $ar value and the width parameter to the $w value, along with face-based fill cropping:

Use the initial height and width (c_lpad,h_ih,w_iw) to center an object in an image after trimming (e_trim):

Arithmetic

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 $newwidth value:

Conditional

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 $height value:

Text overlay

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 ($style and $color). The text overlay is placed near the bottom of the image on a semi-transparent, rounded border.

Named transformation

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:

c_thumb,g_face,h_$width_div_0.78,w_$width,z_0.6/bo_13px_solid_white/bo_2px_solid_gray

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.

Image overlay in a named transformation

Create a named transformation called image_ratings with the following definition:
c_fill,h_400,w_400/l_$rating/c_scale,w_100/fl_layer_apply,g_south_east,x_20,y_20

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:

File reference variable

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 casual.jpg image.

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 variables.get("overlay"):

Self-referencing variable

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4