Libraries
imgix-rb build
- Installation
- Usage
- Srcset Generation
- Purge Cache
- URL encoding and signed imgix URLs
- What is the
ixlib
param on every request? - Contributing
- License
Installation
Add this line to your application's Gemfile:
And then execute:
$ bundle
Or install it yourself as:
$ gem install imgix
Usage
Initialize a client with a :domain
and your :secure_url_token
. By default, HTTPS URLs are generated, but you can toggle that by passing use_https: false
.
Call Imgix::Client#path
with the resource path to get an Imgix::Path
object back. You can then manipulate the path parameters, and call Imgix#Path#to_url
when you're done.
To disable path encoding, pass {disable_path_encoding: true}
as the second argument to the Imgix#Path#to_url
function.
Srcset Generation
The imgix gem allows for generation of custom srcset
attributes, which can be invoked through Imgix::Path#to_srcset
. By default, the srcset
generated will allow for responsive size switching by building a list of image-width mappings.
Will produce the following attribute value, which can then be served to the client:
Fixed image rendering
In cases where enough information is provided about an image's dimensions, to_srcset
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
or h
. By invoking to_srcset
with either a width or height provided, a different srcset
will be generated for a fixed-size image instead.
Will produce the following attribute value:
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 integers via widths
to the options
keyword argument.
Will generate the following srcset
of width pairs:
Please 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 width_tolerance
are passed to the options
parameter in the to_srcset
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 reducing rendering artifacts), 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 passing a positive numeric value to width_tolerance
within the options
keyword argument:
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
If the exact number of minimum/maximum physical pixels that an image will need to be rendered at is known, a user can specify them by passing an integer via min_width
and/or max_width
to the options
keyword parameters:
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_width: 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.
Also please note that according to the imgix API, the maximum renderable image width is 8192 pixels.
Variable Qualities
This gem 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 options: { disable_variable_quality: true }
to Imgix:Path#to_srcset
.
This behavior specifically occurs when a fixed-size image is rendered, for example:
will generate a srcset with the following q
to dpr
mapping:
Purge Cache
If you need to remove or update an image on imgix, you can purge it from our cache by initializing a client with your API key, then calling Imgix::Client#purge
with the resource path.
To learn more about purging assets with imgix, see our docs.
URL encoding and signed imgix URLs
Some important third parties (like Facebook) apply URL escaping to query string components, which can cause correctly signed imgix URLs to to be transformed into incorrectly signed ones. We URL encode the query part of the URL before signing, so you don't have to worry about this.
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 including include_library_param: false
in the instantiation Hash parameter for Imgix::Client
:
Contributing
See the contributing guide.
License
More Core Libraries
- imgix-csharpbuildA C# client library for generating image URLs with imgixC# 14 a year ago
- imgix-gobuildA Go client library for generating image URLs with imgixGo 12 a year ago
- imgix-javabuildA Java client library for generating URLs with imgixJava 19 a year ago
- imgix-phpbuildA PHP client library for generating URLs with imgixPHP 111 a year ago
- imgix-pythonbuildA Python client library for generating URLs with imgixPython 36 a year ago
- imgix-swiftbuildA Swift client library for generating URLs with imgixSwift 25 9 months ago
- @imgix/js-corebuildA JavaScript client library for generating image URLs with imgixJavaScript 120 9 days ago