Skip to content

feat: add srcset generation#48

Merged
sherwinski merged 16 commits into
masterfrom
srcset-generation
Aug 22, 2019
Merged

feat: add srcset generation#48
sherwinski merged 16 commits into
masterfrom
srcset-generation

Conversation

@sherwinski
Copy link
Copy Markdown
Contributor

@sherwinski sherwinski commented Jul 30, 2019

This PR creates a new class method in UrlBuilder that will generate a srcset string, which can be used in the srcset attribute on an <img> HTML element. create_srcset() takes a path and params argument similar to create_url(), and will return a String in one of two srcset formats.

Srcset Width-Pairs
The first format creates srcset width-pairs, a comma-delimited list of URLs and width descriptors corresponding to each one. This allows for responsive size switching based on the current width of the browser viewport. create_srcset() will append any params passed to the method to each URL constructed within the list. The following is an example of how to generate a width-pair srcset:

builder = imgix.UrlBuilder("demos.imgix.net", include_library_param=False)
srcset = builder.create_srcset("image.png")
print srcset

will return the following String (collapsed for brevity):

https://demos.imgix.net/image.png?w=100 100w,
https://demos.imgix.net/image.png?w=116 116w,
https://demos.imgix.net/image.png?w=134 134w,
                         ...
https://demos.imgix.net/image.png?w=7400 7400w,
https://demos.imgix.net/image.png?w=8192 8192w

Notice that a w= parameter is automatically inserted for each entry in the srcset list. This is required in order for the element to properly display the correctly-sized image corresponding to the viewport's width.

Device Pixel Ratio (DPR) Srcset
The second format this method can return is a srcset list of same-size images in varying resolutions. In this case, images are scaled using the dpr= parameter to adjust for the device pixel ratio of the browser. A DPR srcset will be automatically generated instead of a width-pair srcset if exact dimensions for the output image are specified, by providing either a w (width) or a h (height) and ar (aspect ratio) in the params argument. The following is an example of how to generate a DPR srcset:

builder = imgix.UrlBuilder("demos.imgix.net", include_library_param=False)
srcset = builder.create_srcset("image.png", {'h':800, 'ar':'3:2', 'fit':'crop'})
print srcset

which will return the following String:

https://demos.imgix.net/image.png?ar=3%3A2&fit=crop&h=800 1x,
https://demos.imgix.net/image.png?ar=3%3A2&fit=crop&h=800 2x,
https://demos.imgix.net/image.png?ar=3%3A2&fit=crop&h=800 3x,
https://demos.imgix.net/image.png?ar=3%3A2&fit=crop&h=800 4x,
https://demos.imgix.net/image.png?ar=3%3A2&fit=crop&h=800 5x

Signing URLs
create_srcset() also supports signing URLs, which can be very useful for generating server-side and then passing it to the client. This will avoid having to expose your imgix source's secure token to the client.

builder = imgix.UrlBuilder("demos.imgix.net", sign_key="my-token", include_library_param=False)
srcset = builder.create_srcset("image.png", {'w':800})
print srcset

generates a srcset with unique signatures for each URL:

https://demos.imgix.net/image.png?w=800&s=333a2140375016c2a6d2cf53e189ed90 1x,
https://demos.imgix.net/image.png?w=800&s=333a2140375016c2a6d2cf53e189ed90 2x,
https://demos.imgix.net/image.png?w=800&s=333a2140375016c2a6d2cf53e189ed90 3x,
https://demos.imgix.net/image.png?w=800&s=333a2140375016c2a6d2cf53e189ed90 4x,
https://demos.imgix.net/image.png?w=800&s=333a2140375016c2a6d2cf53e189ed90 5x

For more information on srcset, building responsive images, and resolution switching:

@sherwinski sherwinski requested a review from nico256 August 1, 2019 04:49
Comment thread imgix/urlbuilder.py Outdated
Comment thread imgix/constants.py Outdated
Comment thread imgix/urlbuilder.py
Comment thread imgix/urlbuilder.py
@sherwinski sherwinski requested a review from nico256 August 22, 2019 00:59
Comment thread imgix/urlbuilder.py Outdated
Comment thread imgix/urlbuilder.py Outdated
Comment thread imgix/urlbuilder.py Outdated
@sherwinski sherwinski merged commit b7c4d4c into master Aug 22, 2019
@sherwinski sherwinski deleted the srcset-generation branch August 22, 2019 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants