Book Contents

Book Index

Image server

Introduction

The image server is software acting as a server, created for efficient distribution of images to e-shop and Web K2 clients and for optimization of the distributed images. The image server is an intermediary between the e-shop client and the web browser of the user visiting the e-shop or using the Web K2 services.

pic_6330.png

Picture: Image server

In typical use from the e-shop, the process is as follows. The user requests a web page of the e-shop. The e-shop web server returns the generated page (HTML code) containing images (IMG tags) with a URL pointing to the image server. As soon as the user's browser encounters an image while rendering the page, it tries to download it from the web server. Because the image URL leads to the image server, the browser makes an HTTP GET request to the image server. The image server provides the image, modifies it (a detailed description of this process follows below) and returns the resulting image to the browser, which displays it in the correct place according to the e-shop template.

The image server implements several functions that optimize processing speed, request handling latency, the amount of transferred data and transfer time. The image server also allows image modifications such as resizing the image, changing the quality level used for image compression, and reformatting the image to another format.

In the standard configuration, the image server is a stateless service, thanks to which it can easily be scaled horizontally. However, the image server can be modified by plugins that may introduce stateful behaviour into it, although this is strongly discouraged.

Book Contents

Book Index

Image delivery process

The image delivery process consists of three main parts that are interconnected. Each part can handle the request or route it to the next part. The individual blocks are described in the following sections.

pic_6331.png

Picture: Image delivery process

Security check

The security check is used mainly to filter out nonsensical requests and large numbers of requests sent with the aim of putting the image server out of operation (DoS attack). The main reason for the existence of this module is the time demands of some operations, such as resizing images or converting them to another format. The security module filters requests for absurdly large images as well as large numbers of requests to change the image resolution to randomly chosen resolutions within a short time.

In the current version, the module is not very configurable and works autonomously with a verified configuration that should be sufficient for all common use cases.

Cache

The cache is used to store the results of processed images so that they can be served quickly next time. The first image is processed in the standard way, but when another request for the same image is handled, the request is no longer routed to further layers (processing and the image source itself); the stored image from the cache is used instead. The image server stores images in operating memory for fast access. The cache size is configurable and significantly affects the RAM consumption of the image server. It is strongly discouraged to set the size of this cache to a value higher than 75% of the server's operating memory, especially if the server is not dedicated to running the image server. The cache size includes only the memory needed to store image data and does not include any metadata that also has to be kept in that memory. Therefore, a sufficient reserve must be taken into account when choosing the size.

Cache eviction method

Currently, the LRU (least recently used) method is implemented, which, when the cache is exhausted, frees the least recently used item from the cache (or more items if the space is still insufficient after removing one).

Image processing

In this part, the image is modified. The part consists of several sub-blocks that perform individual modifications based on their configuration and the request parameters. These are chained blocks that pass the request on and process the obtained image. They are, for example, blocks performing conversion to another format, resizing or configuration of the output quality level.

Obtaining images from the source

The last part is the one that provides the image from the source. Currently there are two kinds of these blocks. One provides images from the local file system and the other provides images used by the K2 HTML5 client. These blocks provide non-optimized images and pass them to the previous parts handling the image delivery process for further processing.

Skipping a part of the image processing

Under certain circumstances, the system can skip a part of the process and continue with the next part. This functionality is not configurable and the system does it only in certain specific situations. An example is the delivery of animated images in GIF format, which cannot be processed and optimized in the conventional way while preserving the animated behaviour.

Book Contents

Book Index

Endpoint

Note: In older versions, the term Target was used instead of the term Endpoint.

One image server can provide images for multiple services, and the configuration is independent for each of them. The configuration includes, for example, the path to the folder from which images will be loaded, etc. For example, the following diagram shows a configuration that contains configuration for 2 e-shops (and 2 instances of Web K2). Each e-shop can have images loaded from a different folder, and different default parameters can be applied to images for each e-shop. Each endpoint also has its own caches (L1 and L2), which can have different parameters (a different size) for each endpoint.

pic_6332.png

Picture: Endpoint

Endpoint configuration

Each endpoint must have a name and an image source specified.

Basic configuration

Configuration of endpoints and of the image server in general is done in the appsettings.json file. The file is in JSON format according to the ECMA-404 standard. Pay particular attention to the fact that the JSON standard does not support comments, although some parsers support them. The parser used for parsing the image server configuration files does not support comments, and any comment in the configuration file will almost always cause its parsing to fail.

The configuration object extends the ASP.NET Core configuration (on which the image server is built), and the ASP.NET Core configuration can also be modified in the configuration file at your own risk. The configuration file consists of a global object and nested nodes. The image server is configured in the ImageServer node. Endpoints are listed in the Targets node within the ImageServer node. Each endpoint configuration must contain two mandatory parameters:

An example configuration of one endpoint is shown in the following sample:

{
	"ImageServer": {
		"Targets": [
			{
				"Target": "eshop",
				"Source": "file://C:\\Program Files (x86)\\K2\\SupportFiles\\DemoPictures",
			}
		]
	}
}

The sample configures an endpoint named eshop. If the image server ran over HTTPS at the address mujobrazkovyserver.cz, the images would be available at https://mujobrazkovyserver.cz/eshop. The configuration uses the file:// protocol as the image source, which loads images from the local file system from the folder given in the parameter. Special characters such as the backslash must be escaped in the path. Multiple endpoints can be defined in the configuration file, as shown in the following sample:

{
	"ImageServer": {
		"Targets": [
			{
				"Target": "eshop",
				"Source": "file://C:\\Program Files (x86)\\K2\\SupportFiles\\DemoPictures",
			},
			{
				"Target": "eshop-test",
				"Source" : " file://C:\\Program Files (x86)\\K2\\SupportFiles\\DemoPictures-test"
			}
		]
	}
}

Supported data sources

The data source can be selected using the protocol identifier in the "Source" element of the endpoint configuration in the configuration file:

Loading images from a local file

To load images from a folder available on the server on which the image server is running, the URI must be formatted so that the protocol is set to file, the standard sequence :// is used as the separator between the protocol and the path, followed by the absolute path to the directory in which images are searched for. In JSON format, some characters, such as the backslash, must be escaped.

Cache configuration

The memory cache size is configurable. The following sample shows changing the cache size to 8 GiB (8589934592 B).

{
	"ImageServer": {
		"Targets": [
			{
				"StandardProviderChainConfiguration": {
					"MemoryCacheSize": "8589934592",
				}
			}
		]
	}
}

Configuration of how parameters are passed in URLs

The image server supports 2 ways of specifying the parameters of the requested output images, which are described below. The format is selected by the RequestParserName parameter in the endpoint configuration node. Valid values are InQueryParameters and InNameParameters. An example configuration setting the request parsing mode to InQueryParameters is shown in the following sample:

{
	"ImageServer": {
		"Targets": [
			{
				"Target": "eshop",
				"Source": "file://C:\\Program Files (x86)\\K2\\SupportFiles\\DemoPictures",
				"RequestParserName": "InQueryParameters"
			}
		]
	}
}

Configuration of forced format conversion

The image server allows forcing conversion of an image to a different output format if the user does not explicitly request a different conversion using a parameter. The image server can, e.g., be configured to always convert from PNG to JPEG. So if a client requests the image test.png, the image server generates an image with the png extension, but its actual content will be a valid image in JPEG format. It is then up to the client to recognize this fact and display the image correctly. The configuration is done by defining a list of formats FROM which this conversion is allowed and one format TO which the images will be converted. An example configuration that converts images in JPEG and PNG formats to WEBP format is shown in the following sample:

{
	"ImageServer": {
		"Targets": [
			{
				"StandardProviderChainConfiguration": {
					"ForceOutputFormating": [
						{
							"RequestedFormats": [ "jpg", "png" ],
							"OutputFormat": "webp"
						}
					]
				},
			}
		]
	}
}

Book Contents

Book Index

Parameters of the provided images

Using parameters in the image URL, the parameters of the returned image can be modified regardless of the parameters of the image stored on the server. Currently there are two ways of entering parameters, with different properties.

  1. As part of the image name before the extension. (e.g.: obr__w100.jpg)
  2. As HTTP parameters after the extension and a question mark. (e.g. obr.jpg?w=100)

The type of accepted parameters is set in the Endpoint configuration by the RequestParserName property.

Passing parameters as part of names

With this approach, the parameters are added between the image name and the extension. The parameters are separated from the name by two underscores, followed by the parameter name and immediately after it the value, if the parameter requires one. Multiple parameters can be specified. In that case they are separated by a single underscore. An example URL of an image with two parameters is shown in the following sample. The meaning of the parameters themselves is described later in this section:

pic_6325.png

Picture: Passing parameters as part of names

Passing parameters as http GET parameters

The second way of passing parameters consists in passing them using standard URL parameters defined by the HTTP protocol. The parameters are written after the file extension, following a question mark. Parameters are separated from each other by an ampersand, and the name and value of a particular parameter by an equals sign.

pic_6326.png

Picture: Passing parameters as http GET parameters

Examples of using parameters modifying the output image

For the following examples, let us have an installed and configured image server available at https://mujobrazkovyserver.cz/ with a configured endpoint named eshop providing images from a folder. The following examples use for demonstration an image that:

The exact sizes depend on the chosen image, and the image server generates differently optimized output images for different images. The following figures are for illustration only.

The image with almost original properties is by default available at the URL:

Name parameters

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek.png

GET parameters

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek.png

A request for this image returns an image that:

Even with a plain request for a file, the image server performs optimizations. The efficiency of the optimization depends on various factors, especially on the kind of image (a photo behaves differently from a screenshot, etc.) and the format (the JPEG format allows significantly better optimization of photographs, etc.). Under normal circumstances, when a suitable format is chosen for the given kind of image, the following efficiency can be expected:

However, the ranges are not strict; they are rather empirical findings. There is even an edge case where the image is larger after optimization than the source image was.

Format conversion

The image server can convert the image format. The conversion can either be requested by a parameter or set globally for certain formats (e.g. always convert png to webp). This section describes conversion forced by a URL parameter. Automatic conversion is described in the Endpoint configuration section. As an example, conversion of the image described above to JPEG format will be shown. The URL is changed by replacing the image extension with the extension to which the image is to be converted. For example, muj_pekny_obrazek.png changes to muj_pekny_obrazek.jpg. However, because the image server cannot distinguish a request for format conversion from a request for an image that is actually named like this on the disk storage, it requires adding the ofmt (original format) parameter, which specifies the extension of the source image.

Example URLs of an image converted from PNG to JPG format:

Parameters in the name

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek__ofmtpng.jpg

Parameters in GET

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek.jpg?ofmt=png

A request for this image returns an image that:

Changing the image size

The image server allows changing the size of the output image by a parameter. If the image is used in a template, the necessary parameter can be added to the image URL to ensure that an unnecessarily large image is not downloaded. The size can be affected by the parameters w (width) and h (height). The parameters cannot be combined. Specifying both parameters at the same time leads to undefined behaviour of the image server. The image size is changed proportionally. An example request that resizes the image so that its width is exactly 800px is shown by the following URLs:

Name parameters

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek__w800.png

GET parameters

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek.png?w=800

A request for this image returns an image that:

Optimization quality setting

For image processing, the image server uses a library that allows configuring the quality level of the output image and thereby improving the efficiency of size optimization. The value can be configured using the qua parameter, set to a value in the range 0–100. If the parameter is not specified, the value 75 is assumed; based on empirical measurements, at this value there is practically no visible degradation of image quality while the transferred data is reduced quite significantly. The result of the optimization depends on the format used and its suitability. When a suitable format is used (PNG for non-photographic data, JPEG for photographic data), good results can be achieved with practically any value in the range 10 – 90. With an unsuitably chosen format (e.g. PNG for photographs), image quality usually drops quickly as the quality setting is lowered, while the volume of transferred data does not decrease. It is recommended to convert to the webp format if the client supports it, because this format is optimized for transferring both photographic and non-photographic data and achieves results similar to the JPEG format for all kinds of data. Some older web browsers, or other viewers displaying images from the image server, do not support it.

An example URL changing the output quality to the value 40 is shown in the following sample:

Name parameters:

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek__qua40.png

GET parameters

https://mujobrazkovyserver.cz/eshop/pekne-obrazky/muj_pekny_obrazek.png?qua=40

A request for this image returns an image that:

Book Contents

Book Index

Operating statistics

The image server can display operating statistics for each configured Endpoint.

Page address

Statistics are available for each endpoint at the address: http(s)://cestak_k_obrazkovemu_server/{Endpoint}/stats.

For example, for an image server available at https://mujobrazkovyserver.cz/ with a configured endpoint named eshop, the statistics for this endpoint are available at: https://mujobrazkovyserver.cz/eshop/stats

Authentication

Authentication is required to access the statistics. Login credentials are defined in the administrators.json file, which is located in the folder where the image server was installed. The default file, which does not define any credentials, has the following content:

{
    "Administrators": [
    ]
}

The installation includes a utility (K2.ImageServer.AdministratorPasswd.exe) that allows generating the configuration for a specific user. In the utility, choose a user name and enter a sufficiently long and secure password.

pic_6327.png

Picture: Utility

The generated output must be copied into the administrators.json configuration file between the square brackets. An example configuration file may look as follows:

{
    "Administrators": [
        {
            "Username": "best_admin",
            "Salt": "B03DEC3D54A1C5D824070782CCF0D0936A0B6620D3F943C62CBBB7E11C0DB10A",
            "Hash": "E106D4F8FB10337EDB95498443067BBB163E776DC1439F7B38B6C5D5F29CCA5B"
        }
    ]
}

To make the statistics accessible to more users, multiple configuration blocks can be inserted into one file. They must be separated by a comma. An example configuration file allowing access for two administrators may look as follows:

{
    "Administrators": [
        {
            "Username": "best_admin",
            "Salt": "B03DEC3D54A1C5D824070782CCF0D0936A0B6620D3F943C62CBBB7E11C0DB10A",
            "Hash": "E106D4F8FB10337EDB95498443067BBB163E776DC1439F7B38B6C5D5F29CCA5B"
        },
        {
            "Username": "profi_admin",
            "Salt": "410F194BB42E6FB722B975AD4618A373A1E4D23510A2A3E8E34F3C04F9DDA5C7",
            "Hash": "11E329DF646E86BBDF5C9A2D2FF719AC785191DA0327DEE868DCD9FD65F475FA"
        }
    ]
}

Page content

The page with cache statistics may look as follows.

pic_6328.png

Picture: Page content

The list shows the individual processing phases and the modules that are part of the given request processing phase. Each module can export a certain kind of statistics. In the current version, only the L1 and L2 caches export statistics. Support for exporting statistics from other modules is planned for the future.

Cache statistics

The cache exports the following statistics: