3.11.0 • Published 6 years ago

westwing-image-transformer v3.11.0

Weekly downloads
3
License
MIT
Repository
-
Last release
6 years ago

Westwing Image Transformer

npm version pipeline status coverage report

The image-transformer transforms images to any required format, style and dimension and also optimizes them for minimal file size alongside high visual quality for an improved user experience and minimal bandwidth.

This library was originally meant to replace Cloudinary image transformations therefore it's historically bound to some of its specific features and requirements.

We try to point those out in this guide.

Tables of content

Url

The URL is the entry point of this library. Beside some preliminary configuration, all that's required to transform an image can be set just by composing a certain URL.

Url parts

URL partDescriptionTypeRequired
/image/upload/(Legacy) Cloudinary Prefixstringyes
/named_transformation/manual_transformation/Sets of transformation. See here for more details.stringno
/v12345678/URL Version. Can be used as a cache-busterstringno
/s3-key/to-image/The s3 key (path) to the origin image.stringyes

Url examples

Some URLs examples to get a quick idea. For all the possibilities, check here.

Original Image

npm.io

Resized Image with auto height - 400 x 300

npm.io

/w_400,h_auto/

Resized Image - 300 x 200

npm.io

/w_300,h_200/

Cropped Image - 600 x 450

npm.io

/c_crop,w_600,h_450/

Image with applied Image and Text Image - 600 x 450

npm.io

/w_600,h_450/g_northeast,t_black_friday_badge,w_65,h_65,y_6,x_6/g_north_east,co_rgb:ffffff,l_text:Roboto_23_medium:-22%2525,x_13,y_29/

Image Transformations Reference

The table on this page lists all transformation parameters currently supported by the image-transformer library. For each parameter you can see the short name that you can use for manually building transformation URLs.

Param nameURL paramValueExampleDescription
width and heightw,hpixelsautow_300,h_450, w_400,h_autoThe required width / height of a transformed image or an overlay.
x and yx,ypixelsx_20,y_20Vertical / Horizontal position for custom-coordinates based cropping and overlay placement.
cropcmodeA crop mode that determines how to transform the image for fitting into the desired width & height dimensions.
scale (default)Change the size of the image exactly to the given width and height without necessarily retaining the original aspect ratio: all original image parts are visible but might be stretched or shrunk.
cropUsed to extract a given width & height out of the original image. The original proportions are retained and so is the size of the graphics.
padResize the image to fill the given width and height while retaining the original aspect ratio. If the proportions of the original image do not match the given width and height, padding is added to the image to reach the required size.
limitThe image is resized so that it takes up as much space as possible within a bounding box defined by the given width and height parameters only if the original image is larger than the given limit. The original aspect ratio is retained and all of the original image is visible.
gravitygdirectionDetermines which part of the image to keep when any part of the image is cropped. For overlays, this setting determines where to place the overlay.
(default)center (applies only to some transformations)
northtop
north_easttop right
north_westtop left
southbottom
south_eastbottom right
south_westbottom left
westright
eastleft
default imagedidentifierSpecify the public ID of a placeholder image to use if the requested image or social network picture does not exist. The name of the placeholder image must include the file extension.
image_keyd_dont-delete:default_error.pngUse default image with the key /dont-delete/default_error.png. (For the bucket where the default image is fetched from check here.
overlaylidentifierCustomize the color to use together with text captions. The color can be set as an RGB.
image_keyl_club:dev:ch:campaign:DEHOWT15:badgeAdd the overlay with the key dev/ch/campaign/DEHOWT15/badge. When using the overlay image transformation the image-transformer assumes the image is located in the sourceBucket.
textl_text:Roboto_23_medium:Hello%20WorldAdd a 'Hello World' text overlay with font-family: Roboto, font-size: 23px, font-weight: medium. As a default behaviour, before being overlay-ed, text is treated as a regular image and can be resized and positioned. Size and position are considered as absolute values. Multiple text overlays are supported and applied following the order in the URL. Note: check here the overall transformations priority.
Defaults font-family: Roboto, font-size: 23px, font-weight: regular.
colorcocolorCustomize the color to use together with text captions. The color can be set as an RGB.
rgbco_rgb:000000In the case of a text transformations, applies the specified color (000000) to the text.
transformationtnameSet one flag that alters the default transformation behavior.
defaultApplies pre-defined set of transformations. For historical reasons, the rule is to have the named transformation occurring after t_default and separated with . (dot).
t_default.upcoming_previewApplies the set of transformations configured in the upcoming_preview. (For transformations definitions check here.
badgeApplies a badge transformation provided a badge name. The rule is to wrap the badge name between t_ and _badge. As a default behaviour, before being overlay-ed, the badge is treated as a regular image and can be resized and positioned. Size and position are considered as absolute values. Multiple badges are supported and are applied following the order in the URL. Note: check here the overall transformations priority.
t_black_friday_badge,h_65,w_65,x_6,y_6Applies the black_friday badge with the given params. (For the badge configuration check here.
flagsflnameApply a pre-defined named transformation of the given name.
relativeWhen applied to text transformations it modifies width or height of below image based on x and y negative params and the origin gravity.
fl_relative,l_text:Hello,x_0,y_-10Applies the text overlay transformation while increasing the height of the image by 10px.
resizelastWhen applied to text or badge overlay transformations it modifies the default transformations priority so that resizing happens last. This means the transformations applied to the overlays will be relative to the image original size.
t_soldout_badge,h_328,w_779,fl_resizelastApplies the specified badge and resizes it relative to the original image.
effectenameApply a filter or an effect on an image.
trime_trimDetect and remove image edges whose color is similar to corner pixels.
qualityqpercentControl the JPEG, WebP, GIF compression quality. 1 is the lowest quality and 100 is the highest. Reducing the quality is a tradeoff between visual quality and file size.
80q_80Sets the quality of images to 80%.
formatfformatDeliver the file in the requested format. In URLs, just set the file extension to the desired format. Available formats: JPEG, PNG, WebP.
pngf_pngConvert the original image to PNG format.

Transformations Definitions

Upon instantiation the image-transformer can be provided with some transformation definitions (also, named transformations). A definition is basically a name for a set of operations which are configured in a Javascript Object like this:

const definitions = {
     'catalog': { 'width': 290, 'height': 240, 'type': 'crop', 'crop': 'pad', 'format': 'jpeg' },
};

const transformer = new Transformer(definitions);

The way to request this named transformation is via the transform (t_) parameter in the URL, like this: /image/upload/t_default.catalog/club/de/campaign/QATEST_up7/l.

Transformations Exclusions

For some cases, it might be useful to ignore some transformations. The most common cases could breaking changes that would require URL updates or when some legacy URL should be supported but avoiding it to break the parser. In cases like those, a Javascript Array of exclusions can be passed as a second argument upon the transformer instantiation.

const exclusions = [
    'l_text',
    '\$bS',
];

const transformer = new Transformer([], exclusions);

This will ignore the URL params containing the exclusions. The check is done via RegEx test.

Transformations Priority

Though the URL params and their orders are held as the source of truth, semantic reasons impose a certain order for the transformations (high prio means applied first). This means that, despite the place the params might appear in the URL, the transformations are sorted so that some of them have priority over others, after which the order of appearance in URL applies.

Default priority

  1. Named Transformations
  2. Manual Transformations
  3. Image Overlay
  4. Text Overlay

Default Image Placeholder

A specific case applies to the Default Image Placeholder, which is only applied whenever the image-transformer is not able to fetch the provided image (or some other error happens during the transformation).

Resize Last

Whenever the flag resizelast is used inside a transformation, it will increase the priority of the transformation to which is applied. This flag is currently supported only for badge and text overlays transformations.

This means that, in a case with URL params like /w_600,h_450/t_black_friday_badge,w_65,h_65/, the resizing will happen before applying the badge.

When using the flag /w_600,h_450/t_black_friday_badge,w_65,h_65,fl_resizelast/, the badge will be applied before resizing.

Note that this has influence over the final overlay size. In the second case the sizes specified with the params will be relative to the original image, meaning that the resulting applied overlay will not have absolute 65px width and height.

Buckets, Custom Assets, Fonts

The image-transformer is dependent on Amazon S3 and a text-to-png; this means that all information about the locations of the related resources have to be provided to the instance.

In order to do this, some convenient methods are provided.

Source and Destination Buckets

The source bucket is the one from which the transformer retrieves the original image and the one used for the l_club transformation.

The destination bucket is the one used to save all transformed image. It is also used to check whether the requested transformation has already been generated.

const transformedImage = await transformer.transformImage(url, originBucket, destBucket);

Custom Assets

The custom assets define the configuration for badges and default placeholder image.

const customAssets = {
    placeholder: {
        bucket: 'placeholder-bucket-name',
    },
    badges: {
        black_friday: {
            badgeBucket: 'badge-bucket-name',
            badgeKey: 'badges/bf.png',
        },
    },
};

const transformer = new Transformer();
transformer.setCustomAssetsLocation(customAssets);

Note that the badge key in this example would correspond to the result of parsing the URL param t_black_friday_badge.

Fonts

The supported fonts are set as default pointing to the /fonts/ in the image-transformer. It is possible however to override this configuration by means of setFontsPath() method, which requires an existent folder path.

Westwing Image Transformer © 2017+, Released under the MIT License. Authored and maintained by Westwing Group AG with help from contributors.

Jakob Houser \ Angelo Gulina 🦄

3.11.0

6 years ago

3.10.10

6 years ago

3.10.9

6 years ago

3.10.8

6 years ago

3.10.7

6 years ago

3.10.6

6 years ago

3.10.5

6 years ago

3.10.4

6 years ago

3.10.3

6 years ago

3.10.2

6 years ago

3.10.1

6 years ago

3.10.0

6 years ago

3.9.10

6 years ago

3.9.9

6 years ago

3.9.8

6 years ago

3.9.7

6 years ago

3.9.6

6 years ago

3.9.5

6 years ago

3.9.4

6 years ago

3.9.3

6 years ago

3.9.2

6 years ago

3.9.1

6 years ago

3.9.0

6 years ago

3.8.0

6 years ago

3.7.0

6 years ago

3.6.0

6 years ago

3.5.0

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.9

6 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.9.6

6 years ago

1.9.5

6 years ago

1.9.4

6 years ago

1.9.3

6 years ago

1.9.2

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.8.1

7 years ago

1.8.0

7 years ago

1.6.9

7 years ago

1.6.8

7 years ago

1.6.7

7 years ago

1.6.6

7 years ago

1.6.5

7 years ago

1.6.4

7 years ago

1.6.3

7 years ago

1.6.2

7 years ago

1.7.0

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.0

7 years ago