Skip to main content

Variables and conditionals

Build image URLs that resolve when they are requested rather than when they are written. Variables hold values that the rest of the query can reuse, conditionals and expressions choose between them, and img404 sets what is served when an asset is missing.

Base URL pattern

The parameters on this page are appended to a Dynamic Media image URL. For the URL pattern and how parameters chain together, see the Media Delivery API guide.

img404 — Missing image fallback
Link copied!

Specifies the filename of the fallback image to display when the requested image cannot be found. The fallback must be a published image on the same account. Can be set as a query parameter, in a transformation template, or at the account level.

The example below requests an image that does not exist - the fallback is served instead:

https://cdn.media.amplience.net/i/ampproduct/this-image-does-not-exist?img404=blue-roundel
Try it...

${variable} — Variables
Link copied!

User-defined variables can be declared in the query string and referenced throughout the query. Variable names are prefixed with $.

In this example, h=$imageheight reads its value from the imageheight variable - change the variable and the height follows:

https://cdn.media.amplience.net/i/ampproduct/woman-checked-skirt-striped-bag?imageheight=300&h=$imageheight
Try it...
300

Conditionals
Link copied!

Conditional expressions evaluate a comparison and return one of two values.

Format: {($variable==value)?trueValue:falseValue}

Supported operators: ==, >, <, >=, <=, !=

Supported types: number, string (must be assigned to a variable first), boolean

In this example the width is 200 when value1 equals value2, and 400 otherwise. Set the two values equal and watch the image shrink:

https://cdn.media.amplience.net/i/ampproduct/woman-happy-shopping-6567737?w={($value1==$value2)?200:400}&value1=1&value2=5
Try it...

Expressions
Link copied!

Mathematical expressions can be included in query values, wrapped in {}.

Supported operators: +, -, *, /.

Variables can be used within expressions, and percentages are also supported - for example, w={25%} returns the image at a quarter of its size, and w={(1/$scalefactor)*100%} scales by the inverse of a variable.

In this example the width resolves to $firstValue + $secondValue - with the defaults, w=300:

https://cdn.media.amplience.net/i/ampproduct/Hero-Banner-720-model2?w={$firstValue+$secondValue}&firstValue=100&secondValue=200
Try it...