imgix / imgix-java

A Java client library for generating URLs with imgix
https://www.imgix.com
BSD 2-Clause "Simplified" License
19 stars 8 forks source link

feat: add srcset generation method #32

Closed sherwinski closed 5 years ago

sherwinski commented 5 years ago

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:

URLBuilder ub = new URLBuilder("test.imgix.net", true, "", false);
HashMap<String,String> params = new HashMap<String,String> ();
params.put("fit", "crop");
params.put("crop", "faces");
System.out.println(ub.createSrcSet("bridge.png", params));

will return the following String (collapsed for brevity):

https://test.imgix.net/bridge.png?crop=faces&fit=crop&w=100 100w,
https://test.imgix.net/bridge.png?crop=faces&fit=crop&w=116 116w,
https://test.imgix.net/bridge.png?crop=faces&fit=crop&w=134 134w,
                                            ...
https://test.imgix.net/bridge.png?crop=faces&fit=crop&w=7400 7400w,
https://test.imgix.net/bridge.png?crop=faces&fit=crop&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:

URLBuilder ub = new URLBuilder("test.imgix.net", true, "", false);
HashMap<String,String> params = new HashMap<String,String> ();
params.put("h", "200");
params.put("ar", "3:2");

System.out.println(ub.createSrcSet("bridge.png", params));

which will return the following String:

https://test.imgix.net/bridge.png?ar=3%3A2&dpr=1&h=200 1x,
https://test.imgix.net/bridge.png?ar=3%3A2&dpr=2&h=200 2x,
https://test.imgix.net/bridge.png?ar=3%3A2&dpr=3&h=200 3x,
https://test.imgix.net/bridge.png?ar=3%3A2&dpr=4&h=200 4x,
https://test.imgix.net/bridge.png?ar=3%3A2&dpr=5&h=200 5x

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

URLBuilder ub = new URLBuilder("test.imgix.net", true, "my-token", false);
HashMap<String,String> params = new HashMap<String,String> ();
params.put("w", "100");
System.out.println(ub.createSrcSet("bridge.png", params));

generates a srcset with unique signatures for each URL:

https://test.imgix.net/bridge.png?dpr=1&w=100&s=f461367db3c5fad8ae9d18864b38533a 1x,
https://test.imgix.net/bridge.png?dpr=2&w=100&s=9cac4c9fa5a263cef1ca443340a64a2c 2x,
https://test.imgix.net/bridge.png?dpr=3&w=100&s=29a0aa19b074a000de96eb4f1a24803b 3x,
https://test.imgix.net/bridge.png?dpr=4&w=100&s=f16ac140640d46ed8499102730e1f62d 4x,
https://test.imgix.net/bridge.png?dpr=5&w=100&s=3ef7c404f749ed5296fe0b6de47d8d2f 5x

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