3 min

September 13, 2024

Optimizing Images Using IPX Provider in Magento 2 and Alokai

In this article, we will cover the process of adding image optimization to one of our websites using the IPX provider We will go over the configuration, how to use it, and important things to consider when implementing it

Listen to the audio version of this article.

elevenlabs cover
Loading the Elevenlabs Text to Speech AudioNative Player...

Vendor configuration

1. Setting up global variables

bash

2. IPX configuration

We have added the following image configuration

where:

  • f_webp - convert to WebP format

  • b_white - set the background color to white

  • quality_100 - image quality 100 (maximum)

  • s_{width}x{height} - set width and height to the specified value. We can also use fit_contain - resize with aspect ratio.

The full URL path looks as follows:

bash

In addition, we will need the getMagentoImage method from VSF. This method returns the URL to the image without the Magento base URL and cache part. We can use it to get images from external providers.

You also need to update the imageSizes helper object, which exports the configuration for different image types, such as:

javascript

Copy code

This is how our method to generate the target path looks like:

javascript

Usage Examples

1. Updating existing code

Example of changing the path to an image:

html

Here you can also see other configurations such as imageComponentTag and fetchpriority that help optimize performance, but they are not the focus of this article.

2.Examples of use in application code

If we want to use this in the code of our application, we simply need to call our method:

javascript

Finishing

Images downloaded by the CDN using IPX were 100-150% smaller, significantly improving site performance. Benefits of using IPX to optimize images:

  1. Reducing image size: Significantly reduce the size of images without sacrificing quality, speeding up page load times.

  2. Automatic conversion to WebP: A modern image format that offers better compression.

  3. Aspect ratio preservation: Automatically adjust the size of images while preserving their aspect ratio.

  4. Ease of configuration: Simple configuration and integration with existing solutions.