feat: add srcset generation#48
Merged
Merged
Conversation
sherwinski
commented
Aug 1, 2019
nico256
reviewed
Aug 2, 2019
nico256
reviewed
Aug 2, 2019
nico256
reviewed
Aug 2, 2019
added 5 commits
August 21, 2019 12:49
…ision between python2 and 3
nico256
reviewed
Aug 22, 2019
nico256
reviewed
Aug 22, 2019
nico256
reviewed
Aug 22, 2019
…l and create_srcset
nico256
approved these changes
Aug 22, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR creates a new class method in
UrlBuilderthat will generate asrcsetstring, which can be used in thesrcsetattribute on an<img>HTML element.create_srcset()takes apathandparamsargument similar tocreate_url(), and will return aStringin one of twosrcsetformats.Srcset Width-Pairs
The first format creates
srcsetwidth-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 anyparamspassed to the method to each URL constructed within the list. The following is an example of how to generate a width-pairsrcset: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 8192wNotice that a
w=parameter is automatically inserted for each entry in thesrcsetlist. 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
srcsetlist of same-size images in varying resolutions. In this case, images are scaled using thedpr=parameter to adjust for the device pixel ratio of the browser. A DPRsrcsetwill be automatically generated instead of a width-pair srcset if exact dimensions for the output image are specified, by providing either aw(width) or ah(height) andar(aspect ratio) in theparamsargument. The following is an example of how to generate a DPRsrcset:which will return the following
String: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.generates a
srcsetwith unique signatures for each URL:For more information on
srcset, building responsive images, and resolution switching: