imgix-core-js
is a JavaScript library for generating image URLs with imgix that can be used in browser or server-side settings.
Installing
imgix-core-js can be installed via npm or bower:
npm install imgix-core-js
or
bower install imgix-core-js
Usage
Depending on your module system, using imgix-core-js is done a few different ways. The most common entry point will be the ImgixClient
class. Whenever you provide data to imgix-core-js, make sure it is not already URL-encoded, as the library handles proper encoding internally.
CommonJS
var ImgixClient = require('imgix-core-js');
var client = new ImgixClient({
domain: 'testing.imgix.net',
secureURLToken: '<SECURE TOKEN>',
});
var url = client.buildURL('/path/to/image.png', {
w: 400,
h: 300,
});
console.log(url);
ES6 Modules
import ImgixClient from 'imgix-core-js';
let client = new ImgixClient({
domain: 'testing.imgix.net',
secureURLToken: '<SECURE TOKEN>',
});
let url = client.buildURL('/path/to/image.png', { w: 400, h: 300 });
console.log(url);
In-browser
var client = new ImgixClient({
domain: 'testing.imgix.net',
});
var url = client.buildURL('/path/to/image.png', { w: 400, h: 300 });
console.log(url);
Configuration
The following options can be used when creating an instance of ImgixClient
:
domain
: String, required. The imgix domain that will be used when constructing URLs. Defaults to null
.useHTTPS
: Boolean. Specifies whether constructed URLs should use the HTTPS protocol. Defaults to true
.includeLibraryParam
: Boolean. Specifies whether the constructed URLs will include an ixlib
parameter. Defaults to true
.secureURLToken
: String. When specified, this token will be used to sign images. Read more about securing images on the imgix Docs site. Defaults to null
.
API
ImgixClient.buildURL(path, params)
path
: String, required. A full, unencoded path to the image. This includes any additional directory information required to locate the image within a source.params
: Object. Any number of imgix rendering API parameters.
Construct a single image URL by passing in the image path
and any rendering API parameters.
var client = new ImgixClient({
domain: 'testing.imgix.net',
});
var url = client.buildURL('folder/image.jpg', {
w: 1000,
});
Returns: an image URL as a string.
https://testing.imgix.net/folder/image.jpg?w=1000&ixlib=js-...
ImgixClient.buildSrcSet(path, params, options)
path
: String, required. A full, unencoded path to the image. This includes any additional directory information required to locate the image within a source.params
: Object. Any number of imgix rendering API parameters.options
: Object. Any number of srcset modifiers, described below:
The imgix-core-js module allows for generation of custom srcset
attributes, which can be invoked through buildSrcSet()
. By default, the srcset
generated will allow for responsive size switching by building a list of image-width mappings.
var client = new ImgixClient({
domain: 'testing.imgix.net',
secureURLToken: 'my-token',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet('image.jpg');
console.log(srcset);
Returns: A srcset
attribute value as a string.
https://testing.imgix.net/image.jpg?w=100&s=e2e581a39c917bdee50b2f8689c30893 100w,
https://testing.imgix.net/image.jpg?w=116&s=836e0bc15da2ad74af8130d93a0ebda6 116w,
https://testing.imgix.net/image.jpg?w=134&s=688416d933381acda1f57068709aab79 134w,
...
https://testing.imgix.net/image.jpg?w=7400&s=91779d82a0e1ac16db04c522fa4017e5 7400w,
https://testing.imgix.net/image.jpg?w=8192&s=59eb881b618fed314fe30cf9e3ec7b00 8192w
Fixed Image Rendering
In cases where enough information is provided about an image's dimensions, buildSrcSet()
will instead build a srcset
that will allow for an image to be served at different resolutions. The parameters taken into consideration when determining if an image is fixed-width are w
, h
, and ar
. By invoking buildSrcSet()
with either a width or the height and aspect ratio (along with fit=crop
, typically) provided, a different srcset
will be generated for a fixed-size image instead.
var client = new ImgixClient({
domain: 'testing.imgix.net',
secureURLToken: 'my-token',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet('image.jpg', {
h: 800,
ar: '3:2',
fit: 'crop',
});
console.log(srcset);
Will produce the following attribute value:
https://testing.imgix.net/image.jpg?h=800&ar=3%3A2&fit=crop&dpr=1&s=3d754a157458402fd3e26977107ade74 1x,
https://testing.imgix.net/image.jpg?h=800&ar=3%3A2&fit=crop&dpr=2&s=a984ad1a81d24d9dd7d18195d5262c82 2x,
https://testing.imgix.net/image.jpg?h=800&ar=3%3A2&fit=crop&dpr=3&s=8b93ab83d3f1ede4887e6826112d60d1 3x,
https://testing.imgix.net/image.jpg?h=800&ar=3%3A2&fit=crop&dpr=4&s=df7b67aa0439588edbfc1c249b3965d6 4x,
https://testing.imgix.net/image.jpg?h=800&ar=3%3A2&fit=crop&dpr=5&s=7c4b8adb733db37d00240da4ca65d410 5x
For more information to better understand srcset
, we highly recommend Eric Portis' "Srcset and sizes" article which goes into depth about the subject.
Custom Widths
In situations where specific widths are desired when generating srcset
pairs, a user can specify them by passing an array of positive integers as widths
to the third options object:
var client = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet(
'image.jpg',
{},
{ widths: [100, 500, 1000, 1800] }
);
console.log(srcset);
Will generate the following srcset
of width pairs:
https://testing.imgix.net/image.jpg?w=100 100w,
https://testing.imgix.net/image.jpg?w=500 500w,
https://testing.imgix.net/image.jpg?w=1000 1000w,
https://testing.imgix.net/image.jpg?w=1800 1800w
Note: that in situations where a srcset
is being rendered as a fixed image, any custom widths
passed in will be ignored. Additionally, if both widths
and a widthTolerance
are passed to the buildSrcSet
method, the custom widths list will take precedence.
Width Tolerance
The srcset
width tolerance dictates the maximum tolerated size difference between an image's downloaded size and its rendered size. For example: setting this value to 0.1 means that an image will not render more than 10% larger or smaller than its native size. In practice, the image URLs generated for a width-based srcset attribute will grow by twice this rate. A lower tolerance means images will render closer to their native size (thereby increasing perceived image quality), but a large srcset list will be generated and consequently users may experience lower rates of cache-hit for pre-rendered images on your site.
By default this rate is set to 8 percent, which we consider to be the ideal rate for maximizing cache hits without sacrificing visual quality. Users can specify their own width tolerance by providing a positive scalar value as widthTolerance
to the third options object:
var client = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet('image.jpg', {}, { widthTolerance: 0.2 });
console.log(srcset);
In this case, the width_tolerance
is set to 20 percent, which will be reflected in the difference between subsequent widths in a srcset pair:
https://testing.imgix.net/image.jpg?w=100 100w,
https://testing.imgix.net/image.jpg?w=140 140w,
https://testing.imgix.net/image.jpg?w=196 196w,
...
https://testing.imgix.net/image.jpg?w=8192 8192w
Minimum and Maximum Width Ranges
In certain circumstances, you may want to limit the minimum or maximum value of the non-fixed srcset
generated by the buildSrcSet()
method. To do this, you can pass in an options object as a third argument, providing positive integers as minWidth
and/or maxWidth
attributes:
var client = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet(
'image.jpg',
{},
{ minWidth: 500, maxWidth: 2000 }
);
console.log(srcset);
Will result in a smaller, more tailored srcset.
https://testing.imgix.net/image.jpg?w=500 500w,
https://testing.imgix.net/image.jpg?w=580 580w,
https://testing.imgix.net/image.jpg?w=672 672w,
https://testing.imgix.net/image.jpg?w=780 780w,
https://testing.imgix.net/image.jpg?w=906 906w,
https://testing.imgix.net/image.jpg?w=1050 1050w,
https://testing.imgix.net/image.jpg?w=1218 1218w,
https://testing.imgix.net/image.jpg?w=1414 1414w,
https://testing.imgix.net/image.jpg?w=1640 1640w,
https://testing.imgix.net/image.jpg?w=1902 1902w,
https://testing.imgix.net/image.jpg?w=2000 2000w
Remember that browsers will apply a device pixel ratio as a multiplier when selecting which image to download from a srcset
. For example, even if you know your image will render no larger than 1000px, specifying options: { max_srcset: 1000 }
will give your users with DPR higher than 1 no choice but to download and render a low-resolution version of the image. Therefore, it is vital to factor in any potential differences when choosing a minimum or maximum range.
Note: that according to the imgix API, the maximum renderable image width is 8192 pixels.
Variable Qualities
This library will automatically append a variable q
parameter mapped to each dpr
parameter when generating a fixed-image srcset. This technique is commonly used to compensate for the increased filesize of high-DPR images. Since high-DPR images are displayed at a higher pixel density on devices, image quality can be lowered to reduce overall filesize without sacrificing perceived visual quality. For more information and examples of this technique in action, see this blog post.
This behavior will respect any overriding q
value passed in as a parameter. Additionally, it can be disabled altogether by passing { disableVariableQuality: true }
to the third argument of buildSrcSet()
.
This behavior specifically occurs when a fixed-size image is rendered, for example:
var client = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false,
});
var srcset = client.buildSrcSet('image.jpg', { w: 100 });
will generate a srcset with the following q
to dpr
mapping:
https://testing.imgix.net/image.jpg?w=100&dpr=1&q=75 1x,
https://testing.imgix.net/image.jpg?w=100&dpr=2&q=50 2x,
https://testing.imgix.net/image.jpg?w=100&dpr=3&q=35 3x,
https://testing.imgix.net/image.jpg?w=100&dpr=4&q=23 4x,
https://testing.imgix.net/image.jpg?w=100&dpr=5&q=20 5x
Web Proxy Sources
If you are using a Web Proxy Source, all you need to do is pass the full image URL you would like to proxy to imgix-core-js
as the path, and include a secureURLToken
when creating the client. imgix-core-js
will then encode this full URL into a format that imgix will understand, thus creating a proxy URL for you.
import ImgixClient from 'imgix-core-js';
const client = new ImgixClient({
domain: 'my-proxy-domain.imgix.net',
secureURLToken: '<token>',
});
client.buildURL('https://example.com/image-to-proxy.jpg', {});
client.buildSrcSet('https://example.com/image-to-proxy.jpg', {});
What is the Ixlib
Param on Every Request?
For security and diagnostic purposes, we sign all requests with the language and version of library used to generate the URL.
This can be disabled by passing a falsy value for the includeLibraryParam
option to new ImgixClient
:
new ImgixClient({
domain: 'my-source.imgix.net',
includeLibraryParam: false,
});
Testing
imgix-core-js uses mocha for testing. Here’s how to run those tests:
npm test