A Ruby gem for generating image URLs with imgix
Ruby 76 a year ago imgix/imgix-rb


Add this line to your application's Gemfile:

gem 'imgix'

And then execute:

$ bundle

Or install it yourself as:

$ gem install imgix


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.

client = '', secure_url_token: 'your-token')

client.path('/images/demo.png').to_url(w: 200)

To disable path encoding, pass {disable_path_encoding: true} as the second argument to the Imgix#Path#to_url function.

client = '', secure_url_token: 'your-token')
client.path('/[images]/demo.png').to_url({},{disable_path_encoding: true})

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.

client = '', secure_url_token: 'your-token')
path = client.path('/images/demo.png')

srcset = path.to_srcset

Will produce the following attribute value, which can then be served to the client: 100w, 116w, 134w,
                                            ... 7400w, 8192w

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.

client = '', secure_url_token: 'your-token')
path = client.path('/images/demo.png')

srcset = path.to_srcset(h:800, ar:'3:2', fit:'crop')

Will produce the following attribute value: 1x, 2x, 3x, 4x, 5x

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.

@client ||= '')
.to_srcset(options: { widths: [100, 500, 1000, 1800] })

Will generate the following srcset of width pairs: 100w, 500w, 1000w, 1800w

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:

client = '', secure_url_token: 'MYT0KEN')
client.path('image.jpg').to_srcset(options: { width_tolerance: 0.20 })

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: 100w, 140w, 196w,
                            ... 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:

client = '')
client.path('image.jpg').to_srcset(options: { min_width: 500, max_width: 2000 })

Will result in a smaller, more tailored srcset. 500w, 580w, 672w, 780w, 906w, 1050w, 1218w, 1414w, 1640w, 1902w, 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:

srcset = '')

will generate a srcset with the following q to dpr mapping: 1x, 2x, 3x, 4x, 5x

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.

client = '', api_key: 'your-key')

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:

client = '', include_library_param: false )


See the contributing guide.


FOSSA Status

More Core Libraries