Skip to content

How to Automatically and Programmatically Update Assets Based on Metadata

Oftentimes you may find yourself flipping between databases, marketing releases, and media assets while trying to remember when one thing changes to update everything else.

Cloudinary makes this process a whole lot easier. Instead of manually create a new version of a product image when pricing, availability, dates, quantity, or anything else changes, you can just reference your asset’s metadata for the text overlays in your on-the-fly transformation, and the changes happen automatically.

Reference the asset’s structured metadata field in your transformation via the standard URL transformation API or via one of Cloudinary’s SDK helper functions by utilizing user-defined variables.

Want to reference an asset’s structured metadata in your transformation and add text layers? Here’s a breakdown:

1. Create a few structured metadata fields. You can create, update, and delete these either via the UI or the Metadata API

For this example, you’ll create three structured metadata fields: price, description, and stock number.

2. Add metadata value specifics of this asset via the UI or API. 

3. Now you’re ready to build your URL on-the-fly transformation. First, declare user-defined variables so that you can reference them later in the transformation. The basic structure of defining metadata as a variable will be ${variable_name}_md:!{metadata_field}!. Afterwards, you can incorporate these variables in a few text layers in the asset.

Here’s a breakdown of each section of the final URL:

    The delivery domain with cloud (testy1), the asset type (image), and the delivery type (upload).
  • $descriptionvar_md:!description/$stock_md:!stock!/$price_md:!price!
    Reference the metadata field `description` and add a user-defined variable that you can reference later with the name `desc`, as well as variables for a few other metadata fields.
  • l_text:verdana_15:$(desc),g_north_west,c_fit,w_160,co_white,b_gray
    This is where you use the variable `desc` that was previously defined. Add a text layer and use the variable for the text content. Then, place it with a gravity transformation; crop the layer; and define its width, background color, and font color. Add layers for price and stock variables as well.
  • shoes_vq5e2g.png
    Finally, specify the public_id of the asset that holds all of the metadata.

After all that hard work, you’re left with the resulting URL:

Loading code examples

The great thing about using structured metadata is that the asset can update automatically without any need to modify the delivery URL. You can also create if statements to programmatically change your asset based on your metadata fields.

For example, if inventory drops below a certain amount, all you have to do is add an if statement to your URL to automatically change the image. You can further automate this process by using Cloudinary’s Metadata API to programmatically link your external database and asset metadata containing product data (e.g., pricing, inventory, etc.).

if_$stock_lt_1/l_text:verdana_100:SOLD%20OUT,co_red/if_endCode language: PHP (php)
Loading code examples

An invalidation request will need to be made when updating existing assets so that the old cached version will be updated.

Using structured metadata fields in your transformations might seem complicated at first, but with a little practice, you can create amazing programmatic assets.

Now that you’ve learned how to reference your asset’s structured metadata field in your transformation, you can quickly and automatically update your assets by combining them with conditional transformations, or simply by shortening your URLs. You could even use Named Transformations to implement your new transformation at scale. There’s no limit to what you can accomplish. 

Back to top

Featured Post