Configure Image Processor on an application
Turn on Image Processor and create a Rules Engine rule that optimizes and caches images by the ims query string, from Console, API v4, or API v3.
You configure Image Processor on an application from Azion Console, the Azion API v4, or the Azion API v3. For a first look at the product, refer to the Image Processor quickstart.
Prerequisites
- An application. To create one, refer to Applications quickstart.
- Access to Azion Console, for the Console procedure. Refer to Access Azion Console.
- A personal token, for the API procedures.
Configure through Azion Console
To turn on Image Processor and Application Accelerator:
Access Azion Console > Applications > your application.
The application runs with both modules. Application Accelerator is required to vary the cache on the ims query string, because the Cache vary by Query String control sits in the cache setting’s Application Accelerator section. It is not required to process an image.
To create the cache setting that gives the derived images a time-to-live (TTL) and varies the cache key by ims:
Access Azion Console > Applications > your application, then go to the Cache Settings tab.
In Name, enter a name that identifies the setting. For example: Images.
Under Browser Cache, select Honor cache policies.
Under Cache, select Override cache behavior.
Set Max Age under Cache to a long duration. Images change less often than other content, so a long duration reduces requests to the origin. Refer to Cache Settings for the maximum you can set.
Under Application Accelerator, open Cache vary by Query String and set Behavior to Allowlist.
In Fields, enter ims.
The cache setting appears in the Cache Settings tab, with a long TTL and the cache key varying by the ims field.
To create the rule that applies the cache setting and optimizes images:
Enter a name for the rule. For example: Optimize images.
In the Criteria section, select the ${request_uri} variable.
Enter \.(jpg|jpeg|gif|bmp|png|ico|webp|avif) as the argument.
Requests whose URI matches the argument carry the cache setting and run through Image Processor. To confirm that an image is optimized, inspect the page with your browser’s developer tools and find the request for the image. The query string carries ims=<width>x<height>, the resolution requested; remove the query string to load the original file.
Configure through the Azion API v4
To turn on Image Processor and Application Accelerator, send a PATCH request, replacing [TOKEN VALUE] with your personal token and <application_id> with your application ID:
Read back, the application carries the following modules:
Application Accelerator is required to vary the cache on the ims query string, because that field belongs to modules.application_accelerator in a cache setting. It is not required to process an image.
To create the cache setting that gives the derived images a time-to-live (TTL) and varies the cache key by ims, send a POST request:
For every field a cache setting carries, refer to Cache Settings.
The response returns the cache setting Azion created:
Copy the id value. To create the rule that applies the cache setting and optimizes images, replacing <cache_setting_id> with the ID you copied, send a POST request:
The response echoes the criteria and behaviors Azion accepted:
Requests whose URI matches the argument carry the cache setting and run through Image Processor. To confirm that an image is optimized, inspect the page with your browser’s developer tools and find the request for the image. The query string carries ims=<width>x<height>, the resolution requested; remove the query string to load the original file.
In the Optimize images for websites and mobile apps use case, this section takes these values:
| Setting | Value |
|---|---|
| Cache setting | images |
| Browser Cache | Honor cache policies (honor in the API) |
| Cache | Override cache behavior, Max Age 31536000 |
| Cache vary by Query String | Allowlist, with ims as the only argument |
The application has a cache setting that stores one object per distinct ims value, for up to a year.
| Setting | Value |
|---|---|
| Name | images - optimize, in the Request Phase |
| Criteria | ${uri} starts with /images/ |
| Behaviors | Set Cache Policy with the images cache setting, then Optimize Images. No Add Request Header behavior |
Every request under /images/ now reaches Image Processor and carries the images cache setting.
Configure through the Azion API v3
To turn on Image Processor and Application Accelerator, send a PATCH request, replacing [TOKEN VALUE] with your personal token and <application_id> with your application ID:
The response returns the updated value.
Application Accelerator is required to vary the cache on the ims query string. It is not required to process an image.
To create the cache setting that gives the derived images a time-to-live (TTL) and varies the cache key by ims, send a POST request:
For every field a cache setting carries, refer to Cache Settings.
The response returns the cache setting Azion created:
Copy the id value. To create the rule that applies the cache setting and optimizes images, replacing <cache_setting_id> with the ID you copied, send a POST request:
The response returns the rule Azion created:
Requests whose URI matches either argument carry the cache setting and run through Image Processor. To confirm that an image is optimized, inspect the page with your browser’s developer tools and find the request for the image. The query string carries ims=<width>x<height>, the resolution requested; remove the query string to load the original file.
Request a fixed set of sizes from your pages
Each distinct ims string is a separate object in cache and a separate transformation on its first request, so pages that ask for a few fixed sizes keep the number of variants small. The example asks for widths of 400, 800, and 1200 pixels with fit-in and quality(85), for images under /images/ on www.example.com. Replace each value with your own.
In the template that renders an image, write the three sizes in the srcset of the img element, so the browser picks the smallest one that fits:
Keep ims as the last argument of the query string. Pages built from the template request one of three variants of each image, and each variant is processed once and then served from cache.
The Optimize images for websites and mobile apps use case uses the values of this example.
Confirm that each variant is processed and cached
Request one size of an image with a browser’s Accept header:
The response carries x-ims: Enabled, content-type: image/webp, and x-original-image-size with the size of the original before the transformation.
Request the same URL twice with the Pragma: azion-debug-cache header:
The second response carries x-cache: HIT, and x-cache-key ends with the ims value and @@webp, the format the variant was converted to. The 400x400 URL, requested with the same headers, has a different x-cache-key, and its first response carries x-cache: MISS. The 800x800 URL requested with -H "Accept: image/jpeg" and the Pragma: azion-debug-cache header carries the original format in content-type, and its x-cache-key carries no @@webp. For how to read the debug headers, refer to Check the cache status of a response.
In the Build and run marketing websites use case, expect:
- Images are processed, when Image Processor is on. Request an image with an
imsquery string, such ashttps://www.example.com/images/hero.jpg?ims=400x. The response carriesx-ims: Enabled.
In the Build e-commerce storefronts use case, expect:
- Product images are processed. An image requested with an
imsquery string, such ashttps://www.example.com/media/blue-shirt.jpg?ims=400x, carriesx-ims: Enabled.
These checks confirm the Optimize images for websites and mobile apps use case.
Next steps
- Image Processor URL parameters - the full
imsgrammar for resize, crop, rotate, filters, and format conversion. - Image Processor settings - every field the module and its cache setting carry, with their values and defaults.
- Applications limits - the maximum size and dimensions Image Processor accepts.
- Cache Settings - every field a cache setting carries, including the maximum TTL.
- Build and run marketing websites - a marketing site deployed from GitHub on every push, served from Object Storage through Cache, and purged after each deploy.
- Build e-commerce storefronts - a storefront whose catalog pages are cached, whose cart and checkout stay dynamic, and whose product pages are purged when the catalog changes.
- Optimize images for websites and mobile apps - an image path that resizes on request, caches every variant, and serves a fixed set of sizes.