Documentation

Best Practices

While there are no strict requirements on how to use ImageEngine, below is a list of best practices to get the most out of ImageEngine.

# Always define display size of images

In your markup, you should always specify a display pixel size (width and height) in the image tags. Either by the width attribute or CSS styles. Additionally, if you’ve implemented Responsive Images on your site, include the sizes attribute. Doing so will prevent unnecessary paints and reflows of the webpage.

In some cases, ImageEngine may return a bigger image than expected to cater to high DPI screens. If the design of the page expects a 400 pixel wide image and ImageEngine returns an 800 pixel wide image for devices with a pixel ratio of 2, the design may be broken if the markup does not define a display size of the image. Similar situations may occur when using Client Hints.

# Origin image in high resolution

If your publishing -system or -workflow supports it, and based on the design of you page, origin images should be of higher resolution than you strictly need. For example, if your design has a hero image with a display width of 1000 pixels (CSS pixels), ImageEngine will try to serve this image at 2000 pixels width for screens with a pixel ratio of 2. Because ImageEngine does not scale images up, the origin image must be at least 2000 physical pixels wide in order to get the best possible quality. See also Client Hints.

# Use Auto mode

In most cases using ImageEngine without directives is the best option. Only use directives for specific cases. When you use directives, some, or all, of the built in optimization logic may not be applied. For example, if you specify /f_webp/ all devices/browsers, even those not supporting webp, will be served a webp image.

# Use Resource Hints

Resource Hints will speed up the connection to ImageEngine by telling the browser as early as possible to connect to the ImageEngine server. This way, the browser will have the connection open when it is needed later. Resource hints are enabled in the <head> section of your markup this:

<link rel="preconnect" href="//images.example.com">

Or alternatively in the HTTP response headers of your web server like this:

Link: <//images.example.com>; rel=preconnect

# Use a CNAME DNS record

Why use a CNAME DNS record? A CNAME DNS record does not expose the origin hostname directly, so it appears more secure. In terms of functionality, there is no difference between using a CNAME’d hostname versus the ImageEngine hostname directly.

# Use Responsive Images

The main reason why ImageEngine benefits from Responsive Images syntax is Client Hints. You can read more about Responsive Images and ImageEngine below.

Summarized, the w_auto directive, and the sizes attribute is key. Here is an example:

<img src="//images.foo.com.imgeng.in/w_auto,800/http://wurfl.github.io/i/pencilsx.jpg" sizes="90vw">

or using the querystring:

<img src="//images.foo.com.imgeng.in/image.jpeg?imgeng=/w_auto,800" sizes="90vw">

If you need different sized images to be served, depending on viewport size, you can use media queries in the sizes attribute.

Usually, the results are over all better when ImageEngine get to decide how to optimize the image. That means no URL parameters or directives are required, just the plain image url:

<img src="//images.foo.com/img.png" alt="Image optimized with ImageEngine" width="100%" sizes="(min-width: 850px) 840px, calc(100vw - 10px)">

Note the presence of the sizes attribute. When sizes is present, the browser is able to calculate the intended display size of the image before making the request to download it. This is the prerequisite for the browser to add client hints to the request. Client hints enable ImageEngine to be much more accurate when resizing and optimizing images.

In your responsive images markup, with or without client hints enabled, it may make sense to use ImageEngine url parameters to request specific sizes of an image:

<img
 src="//images.yoursite.com/camera.jpg"
 sizes="(min-width: 850px) 840px, calc(100vw - 10px)"
 srcset="//images.yoursite.com/camera.jpg?imgeng=/w_375 375w,
         //images.yoursite.com/camera.jpg?imgeng=/w_768 768w"
 alt="ImageEngine with srcset">

Note: ImageEngine will still convert to the best image format whether it is webp, jpeg2000 or something else.

# Use Client Hints

Client Hints enable the browser to send more detailed information about what size the image should be, relative to the viewport size.

To enable Client Hints you’ll have to explicitly enable it by adding the below <meta> tag to your <head> element:

<meta http-equiv="Accept-CH" content="DPR, Viewport-Width, Width">

or adding a HTTP header in the response of the html:

Accept-CH: DPR, Width, Viewport-Width

Using client hints and ImageEngine with responsive images will generate a perfectly sized and optimized image. However, note that the Width header is only sent if the browser is able to determine the intended display size of the image. Hence, you must provide a sizes attribute to the image tags. ImageEngine will only return Content-DPR when Width header is present in the request. If the origin image is smaller than the intended display size, Content-DPR will be < 0 because ImageEngine does not scale images larger than the original size.

Note: the above recommendations are based on the way Client Hints are implemented in the wild and the way ImageEngine has implemented client hints. The Client Hint specification is a living standard, and the current version is different from the version implemented in ImageEngine.

The Client Hints specification can be found here. For more details, please read this article on Client Hints, Responsive Images and ImageEngine.

Other resources: