Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add srcset generation #47

Merged
merged 10 commits into from
Aug 29, 2019
Merged

feat: add srcset generation #47

merged 10 commits into from
Aug 29, 2019

Conversation

sherwinski
Copy link
Contributor

@sherwinski sherwinski commented Aug 27, 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. createSrcSet() takes a path and params argument similar to createURL(), 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. createSrcSet() 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 = new UrlBuilder("demos.imgix.net", true, "", false);
echo $builder->createSrcSet("bridge.png");

will return the following String (collapsed for brevity):

https://demos.imgix.net/bridge.png?w=100 100w,
https://demos.imgix.net/bridge.png?w=116 116w,
https://demos.imgix.net/bridge.png?w=134 134w,
                                    ...
https://demos.imgix.net/bridge.png?w=7400 7400w,
https://demos.imgix.net/bridge.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 = new UrlBuilder("demos.imgix.net", true, "", false);
echo $builder->createSrcSet("bridge.png", array("h"=>800, "ar"=>"3:2", "fit"=>"crop"));

which will return the following String:

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

Signing URLs
createSrcSet() 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 = new UrlBuilder("demos.imgix.net", true, "my-key", false);
echo $builder->createSrcSet("bridge.png", array("w"=>800));

generates a srcset with unique signatures for each URL:

https://demos.imgix.net/bridge.png?dpr=1&w=800&s=39eb37ad41acf7170343aa463424ae49 1x,
https://demos.imgix.net/bridge.png?dpr=2&w=800&s=a8ab13a2c7a17b91db42cb86e45f7c9d 2x,
https://demos.imgix.net/bridge.png?dpr=3&w=800&s=8fefe5daf312f04fb6912a101afbf704 3x,
https://demos.imgix.net/bridge.png?dpr=4&w=800&s=74a6167d6ef8ba410109feda814b9ac0 4x,
https://demos.imgix.net/bridge.png?dpr=5&w=800&s=4449b7f44ba7d6d0527a16d9a10b6e39 5x

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

@sherwinski sherwinski requested a review from cindy-imgix August 28, 2019 20:20
src/Imgix/UrlBuilder.php Outdated Show resolved Hide resolved
src/Imgix/UrlBuilder.php Outdated Show resolved Hide resolved
src/Imgix/UrlBuilder.php Outdated Show resolved Hide resolved
src/Imgix/UrlBuilder.php Outdated Show resolved Hide resolved
src/Imgix/UrlBuilder.php Show resolved Hide resolved
@sherwinski sherwinski merged commit dfebd3e into master Aug 29, 2019
@sherwinski sherwinski deleted the srcset-generation branch August 29, 2019 23:17
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