imgix-rails 3.1.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +59 -13
- data/imgix-rails.gemspec +7 -1
- data/lib/imgix/rails.rb +0 -1
- data/lib/imgix/rails/picture_tag.rb +17 -10
- data/lib/imgix/rails/tag.rb +12 -65
- data/lib/imgix/rails/url_helper.rb +45 -18
- data/lib/imgix/rails/version.rb +1 -1
- data/lib/imgix/rails/view_helper.rb +4 -4
- metadata +8 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dbb6ad0974d411fd8ab3e7167b211234fc0e96c
|
4
|
+
data.tar.gz: 0c2717ac27d3323badcc525148188c9716336d2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 924e0acf93ae7d7a6b824109261d0fd0e5370323655827c346e5b58e2018ad8341f7e013ddf1a1f402df6aca1da67257fdd6b4fad4d1b08d7e60a644b318cc88
|
7
|
+
data.tar.gz: 5271ff9cd004e1fe04c7b6ed2d4682891500b7ccb77e3f39229366d83fe162c95f6f0540976f3d52377556cf068a36e6844a40f1aa75de1697488643169aa5d7
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,24 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
# [4.0.0](https://github.com/imgix/imgix-rb/compare/3.1.0...4.0.0) - December 03, 2019
|
7
|
+
|
8
|
+
The v4.0.0 release of imgix-rails introduces a variety of improvements relating to how this gem handles and generates `srcset` attributes. However, in releasing this version there are some significant interface/behavioral changes that users need to be aware of. Users should note that the `min_width` and `max_width` fields (passed via `tag_options`), as well as the `widths` field, have all been moved to their own encompassing `srcset_options` field. This is done with the intention of providing a more organized and intuitive experience when fine-tuning how `srcset` width pairs are generated. See the following example demonstrating this new pattern:
|
9
|
+
|
10
|
+
```erb
|
11
|
+
<%= ix_image_tag('/unsplash/hotairballoon.jpg',
|
12
|
+
srcset_options: { min_width: 1000, max_width: 2500},
|
13
|
+
tag_options: { alt: 'A hot air balloon on a sunny day' }) %>
|
14
|
+
```
|
15
|
+
|
16
|
+
For users migrating to version 4.0 or later, it is important that all srcset-related modifiers be passed via `srcset_options`, as doing so through `tag_options` or `widths` directly will result in errors. For more details on these modifiers, please see the [ix_image_tag](https://github.com/imgix/imgix-rails#ix_image_tag) or [ix_picture_tag](https://github.com/imgix/imgix-rails#ix_picture_tag) sections.
|
17
|
+
|
18
|
+
In addition to these changes, imgix-rails is now capable of producing [fixed-image srcsets](https://github.com/imgix/imgix-rb#fixed-image-rendering). Users should note that when certain dimension information is provided, imgix-rails will produce a `srcset` at different screen resolutions rather than the typical width pairs. This feature provides expanded functionality to cover more `srcset` use cases that users can take advantage of. We are always happy to provide our users with more tools to assist them in their efforts to build out responsive images on the web.
|
19
|
+
|
20
|
+
* feat: utilize Imgix::Path#to_srcset when constructing srcsets ([#83](https://github.com/imgix/imgix-rails/pull/83))
|
21
|
+
* chore: remove deprecated domain sharding behavior ([#80](https://github.com/imgix/imgix-rails/pull/80))
|
22
|
+
* fix: deprecate resizing height when maintaining aspect ratio ([#78](https://github.com/imgix/imgix-rails/pull/78))
|
23
|
+
|
6
24
|
## [3.1.0](https://github.com/imgix/imgix-rb/compare/3.0.2...3.1.0) - October 25, 2019
|
7
25
|
|
8
26
|
* Update bundler dev dependency to include new major version 2.x ([#71](https://github.com/imgix/imgix-rb/pull/71))
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
imgix is a real-time image processing service and CDN. It allows you to manipulate images merely by changing their URL parameters. For a full list of URL parameters, please see the [imgix URL API documentation](https://www.imgix.com/docs/reference).
|
8
8
|
|
9
|
-
We recommend using something like [Paperclip](https://github.com/thoughtbot/paperclip), [Refile](https://github.com/refile/refile), [Carrierwave](https://github.com/carrierwaveuploader/carrierwave), or [
|
9
|
+
We recommend using something like [Paperclip](https://github.com/thoughtbot/paperclip), [Refile](https://github.com/refile/refile), [Carrierwave](https://github.com/carrierwaveuploader/carrierwave), or [Active Storage](https://github.com/rails/rails/tree/master/activestorage) to handle uploads. After they've been uploaded, you can then serve them using this gem.
|
10
10
|
|
11
11
|
* [Installation](#installation)
|
12
12
|
* [Usage](#usage)
|
@@ -19,6 +19,7 @@ We recommend using something like [Paperclip](https://github.com/thoughtbot/pape
|
|
19
19
|
* [Paperclip and CarrierWave](#paperclip-and-carrierwave)
|
20
20
|
* [Refile](#refile)
|
21
21
|
* [Active Storage](#activestorage)
|
22
|
+
* [Upgrade Guides](#upgrade-guides)
|
22
23
|
* [Development](#development)
|
23
24
|
* [Contributing](#contributing)
|
24
25
|
|
@@ -57,18 +58,17 @@ end
|
|
57
58
|
|
58
59
|
The following configuration flags will be respected:
|
59
60
|
|
60
|
-
- `:
|
61
|
-
- `:
|
62
|
-
- `:
|
63
|
-
- `:
|
64
|
-
- `:shard_strategy` Specify [domain sharding strategy](https://github.com/imgix/imgix-rb#domain-sharded-urls) with multiple sources. Acceptable values are `:cycle` and `:crc`. `:crc` is used by default.
|
61
|
+
- `use_https`: toggles the use of HTTPS. Defaults to `true`
|
62
|
+
- `source`: a String or Array that specifies the imgix Source address. Should be in the form of `"assets.imgix.net"`.
|
63
|
+
- `srcset_width_tolerance`: an optional numeric value determining the maximum tolerance allowable, between the downloaded dimensions and rendered dimensions of the image (default `0.08` i.e. `8%`).
|
64
|
+
- `secure_url_token`: an optional secure URL token found in your dashboard (https://dashboard.imgix.com) used for signing requests
|
65
65
|
|
66
66
|
#### Multi-source configuration
|
67
67
|
|
68
68
|
In addition to the standard configuration flags, the following options can be used for multi-source support.
|
69
69
|
|
70
|
-
- `:
|
71
|
-
- `:
|
70
|
+
- `sources`: a Hash of imgix source-secure_url_token key-value pairs. If the value for a source is `nil`, URLs generated for the corresponding source won't be secured. `sources` and `source` *cannot* be used together.
|
71
|
+
- `default_source`: optionally specify a default source for generating URLs.
|
72
72
|
|
73
73
|
Example:
|
74
74
|
|
@@ -87,16 +87,21 @@ end
|
|
87
87
|
<a name="ix_image_tag"></a>
|
88
88
|
### ix_image_tag
|
89
89
|
|
90
|
-
The `ix_image_tag` helper method makes it easy to pass parameters to imgix to handle resizing, cropping, etc. It also simplifies adding responsive imagery to your Rails app by automatically generating a `srcset` based on the parameters you pass. We talk a bit about using the `srcset` attribute in an application in the following blog post: [“Responsive Images with `srcset` and imgix.”](https://
|
90
|
+
The `ix_image_tag` helper method makes it easy to pass parameters to imgix to handle resizing, cropping, etc. It also simplifies adding responsive imagery to your Rails app by automatically generating a `srcset` based on the parameters you pass. We talk a bit about using the `srcset` attribute in an application in the following blog post: [“Responsive Images with `srcset` and imgix.”](https://docs.imgix.com/tutorials/responsive-images-srcset-imgix?_ga=utm_medium=referral&utm_source=sdk&utm_campaign=rails-readme).
|
91
91
|
|
92
|
-
`ix_image_tag` generates `<img>` tags with a filled-out `srcset` attribute that leans on imgix to do the hard work.
|
92
|
+
`ix_image_tag` generates `<img>` tags with a filled-out `srcset` attribute that leans on [imgix-rb](https://github.com/imgix/imgix-rb) to do the hard work. It also makes a variety of options available for customizing how the `srcset` is generated. For example, if you already know the minimum or maximum number of physical pixels that this image will need to be displayed at, you can pass the `min_width` and/or `max_width` options. This will result in a smaller, more tailored `srcset`.
|
93
93
|
|
94
94
|
`ix_image_tag` takes the following arguments:
|
95
95
|
|
96
|
-
* `source`:
|
96
|
+
* `source`: An optional String indicating the source to be used. If unspecified `:source` or `:default_source` will be used. If specified, the value must be defined in the config.
|
97
97
|
* `path`: The path or URL of the image to display.
|
98
|
-
* `tag_options`:
|
98
|
+
* `tag_options`: HTML attributes to apply to the generated `img` element. This is useful for adding class names, alt tags, etc.
|
99
99
|
* `url_params`: The imgix URL parameters to apply to this image. These will be applied to each URL in the `srcset` attribute, as well as the fallback `src` attribute.
|
100
|
+
* `srcset_options`: A variety of options that allow for fine tuning `srcset` generation. More information on each of these modifiers can be found in the [imgix-rb documentation](https://github.com/imgix/imgix-rb#srcset-generation). Any of the following can be passed as arguments:
|
101
|
+
* [`widths`](https://github.com/imgix/imgix-rb#custom-widths): An array of exact widths that `srcset` pairs will be generated with.
|
102
|
+
* [`min_width`](https://github.com/imgix/imgix-rb#minimum-and-maximum-width-ranges): The minimum width that `srcset` pairs will be generated with. Will be ignored if `widths` are provided.
|
103
|
+
* [`max_width`](https://github.com/imgix/imgix-rb#minimum-and-maximum-width-ranges): The maximum width that `srcset` pairs will be generated with. Will be ignored if `widths` are provided.
|
104
|
+
* [`disable_variable_quality`](https://github.com/imgix/imgix-rb#variable-qualities): Pass `true` to disable variable quality parameters when generating a `srcset` ([fixed-images only](https://github.com/imgix/imgix-rails#fixed-image-rendering)). In addition, imgix-rails will respect an overriding `q` (quality) parameter if one is provided through `url_params`.
|
100
105
|
|
101
106
|
```erb
|
102
107
|
<%= ix_image_tag('/unsplash/hotairballoon.jpg', url_params: { w: 300, h: 500, fit: 'crop', crop: 'right'}, tag_options: { alt: 'A hot air balloon on a sunny day' }) %>
|
@@ -143,9 +148,28 @@ Then rendering the portrait in your application is very easy:
|
|
143
148
|
If you already know all the exact widths you need images for, you can specify that by passing the `widths` option as an array. In this case, imgix-rails will only generate `srcset` pairs for the specified `widths`.
|
144
149
|
|
145
150
|
```erb
|
146
|
-
<%= ix_image_tag('/unsplash/hotairballoon.jpg', widths: [320, 640, 960, 1280]
|
151
|
+
<%= ix_image_tag('/unsplash/hotairballoon.jpg', srcset_options: { widths: [320, 640, 960, 1280] }, tag_options: { alt: 'A hot air balloon on a sunny day' }) %>
|
152
|
+
```
|
153
|
+
|
154
|
+
#### Fixed image rendering
|
155
|
+
|
156
|
+
In cases where enough information is provided about an image's dimensions, `ix_image_tag` 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`, `h`, and `ar`. By invoking `ix_image_tag` with either a width or the height and aspect ratio (along with `fit=crop`, typically) provided, a different srcset will be generated for a fixed-size image instead.
|
157
|
+
|
158
|
+
```erb
|
159
|
+
<%= ix_image_tag('/unsplash/hotairballoon.jpg', url_params: {w: 1000}) %>
|
160
|
+
```
|
161
|
+
|
162
|
+
Will render the following HTML:
|
163
|
+
|
164
|
+
```html
|
165
|
+
<img srcset="https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000&dpr=1&q=75 1x,
|
166
|
+
https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000&dpr=2&q=50 2x,
|
167
|
+
https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000&dpr=3&q=35 3x,
|
168
|
+
https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000&dpr=4&q=23 4x,
|
169
|
+
https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000&dpr=5&q=20 5x" sizes="100vw" src="https://assets.imgix.net/image.jpg?ixlib=rails-3.0.2&w=1000">
|
147
170
|
```
|
148
171
|
|
172
|
+
Fixed image rendering will automatically append a variable `q` parameter mapped to each `dpr` parameter when generating a `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](https://blog.imgix.com/2016/03/30/dpr-quality?_ga=utm_medium=referral&utm_source=sdk&utm_campaign=rails-readme). This behavior will respect any overriding `q` value passed in via `url_params` and can be disabled altogether with `srcset_options: { disable_variable_quality: true }`.
|
149
173
|
|
150
174
|
<a name="ix_picture_tag"></a>
|
151
175
|
### ix_picture_tag
|
@@ -159,6 +183,11 @@ The `ix_picture_tag` helper method makes it easy to generate `picture` elements
|
|
159
183
|
* `tag_options`: Any options to apply to the parent `picture` element. This is useful for adding class names, etc.
|
160
184
|
* `url_params`: Default imgix options. These will be used to generate a fallback `img` tag for older browsers, and used in each `source` unless overridden by `breakpoints`.
|
161
185
|
* `breakpoints`: A hash describing the variants. Each key must be a media query (e.g. `(max-width: 880px)`), and each value must be a hash of parameter overrides for that media query. A `source` element will be generated for each breakpoint specified.
|
186
|
+
* `srcset_options`: A variety of options that allow for fine tuning `srcset` generation. More information on each of these modifiers can be found in the [imgix-rb documentation](https://github.com/imgix/imgix-rb#srcset-generation). Any of the following can be passed as arguments:
|
187
|
+
* [`widths`](https://github.com/imgix/imgix-rb#custom-widths): An array of exact widths that `srcset` pairs will be generated with.
|
188
|
+
* [`min_width`](https://github.com/imgix/imgix-rb#minimum-and-maximum-width-ranges): The minimum width that `srcset` pairs will be generated with. Will be ignored if `widths` are provided.
|
189
|
+
* [`max_width`](https://github.com/imgix/imgix-rb#minimum-and-maximum-width-ranges): The maximum width that `srcset` pairs will be generated with. Will be ignored if `widths` are provided.
|
190
|
+
* [`disable_variable_quality`](https://github.com/imgix/imgix-rb#variable-qualities): Pass `true` to disable variable quality parameters when generating a `srcset` ([fixed-images only](https://github.com/imgix/imgix-rails#fixed-image-rendering)). In addition, imgix-rails will respect an overriding `q` (quality) parameter if one is provided through `url_params`.
|
162
191
|
|
163
192
|
```erb
|
164
193
|
<%= ix_picture_tag('bertandernie.jpg',
|
@@ -345,6 +374,23 @@ Finally, the two can be used together by passing in the filename of the ActiveSt
|
|
345
374
|
<%= ix_image_tag(@your_model.image.key) %>
|
346
375
|
```
|
347
376
|
|
377
|
+
<a name="upgrade-guides"></a>
|
378
|
+
## Upgrade Guides
|
379
|
+
|
380
|
+
### 3.x to 4.0
|
381
|
+
|
382
|
+
The v4.0.0 release of imgix-rails introduces a variety of improvements relating to how this gem handles and generates `srcset` attributes. However, in releasing this version there are some significant interface/behavioral changes that users need to be aware of. Users should note that the `min_width` and `max_width` fields (passed via `tag_options`), as well as the `widths` field, have all been moved to their own encompassing `srcset_options` field. This is done with the intention of providing a more organized and intuitive experience when fine-tuning how `srcset` width pairs are generated. See the following example demonstrating this new pattern:
|
383
|
+
|
384
|
+
```erb
|
385
|
+
<%= ix_image_tag('/unsplash/hotairballoon.jpg',
|
386
|
+
srcset_options: { min_width: 1000, max_width: 2500},
|
387
|
+
tag_options: { alt: 'A hot air balloon on a sunny day' }) %>
|
388
|
+
```
|
389
|
+
|
390
|
+
For users migrating to version 4.0 or later, it is important that all srcset-related modifiers be passed via `srcset_options`, as doing so through `tag_options` or `widths` directly will result in errors. For more details on these modifiers, please see the [ix_image_tag](https://github.com/imgix/imgix-rails#ix_image_tag) or [ix_picture_tag](https://github.com/imgix/imgix-rails#ix_picture_tag) sections.
|
391
|
+
|
392
|
+
In addition to these changes, imgix-rails is now capable of producing [fixed-image srcsets](https://github.com/imgix/imgix-rb#fixed-image-rendering). Users should note that when certain dimension information is provided, imgix-rails will produce a `srcset` at different screen resolutions rather than the typical width pairs. This feature provides expanded functionality to cover more `srcset` use cases that users can take advantage of. We are always happy to provide our users with more tools to assist them in their efforts to build out responsive images on the web.
|
393
|
+
|
348
394
|
<a name="development"></a>
|
349
395
|
## Development
|
350
396
|
|
data/imgix-rails.gemspec
CHANGED
@@ -14,6 +14,12 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.description = %q{Makes integrating imgix into your Rails app easier. It builds on imgix-rb to offer a few Rails-specific interfaces. Please see https://github.com/imgix/imgix-rails for more details.}
|
15
15
|
spec.homepage = "https://github.com/imgix/imgix-rails"
|
16
16
|
|
17
|
+
spec.metadata = {
|
18
|
+
'bug_tracker_uri' => 'https://github.com/imgix/imgix-rails/issues',
|
19
|
+
'changelog_uri' => 'https://github.com/imgix/imgix-rails/blob/master/CHANGELOG.md',
|
20
|
+
'documentation_uri' => "https://www.rubydoc.info/gems/imgix-rails/#{spec.version}",
|
21
|
+
'source_code_uri' => "https://github.com/imgix/imgix-rails/tree/v#{spec.version}"
|
22
|
+
}
|
17
23
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
24
|
# delete this section to allow pushing this gem to any host.
|
19
25
|
if spec.respond_to?(:metadata)
|
@@ -27,7 +33,7 @@ Gem::Specification.new do |spec|
|
|
27
33
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
34
|
spec.require_paths = ["lib"]
|
29
35
|
|
30
|
-
spec.add_runtime_dependency "imgix", "~>
|
36
|
+
spec.add_runtime_dependency "imgix", "~> 3.0"
|
31
37
|
|
32
38
|
spec.add_development_dependency "bundler", ">=1.9"
|
33
39
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/imgix/rails.rb
CHANGED
@@ -1,35 +1,42 @@
|
|
1
1
|
require "imgix/rails/tag"
|
2
2
|
require "imgix/rails/image_tag"
|
3
|
+
require "action_view"
|
3
4
|
|
4
5
|
class Imgix::Rails::PictureTag < Imgix::Rails::Tag
|
5
6
|
include ActionView::Context
|
6
7
|
|
7
|
-
def initialize(path, source: nil, tag_options: {}, url_params: {}, breakpoints: {},
|
8
|
+
def initialize(path, source: nil, tag_options: {}, url_params: {}, breakpoints: {}, srcset_options: {})
|
8
9
|
@path = path
|
9
10
|
@source = source
|
10
11
|
@tag_options = tag_options
|
11
12
|
@url_params = url_params
|
12
13
|
@breakpoints = breakpoints
|
13
|
-
@
|
14
|
+
@srcset_options = srcset_options
|
14
15
|
end
|
15
16
|
|
16
17
|
def render
|
17
18
|
content_tag(:picture, @tag_options) do
|
18
19
|
@breakpoints.each do |media, opts|
|
20
|
+
validate_opts(opts)
|
21
|
+
|
19
22
|
source_tag_opts = opts[:tag_options] || {}
|
20
23
|
source_tag_url_params = opts[:url_params] || {}
|
21
|
-
|
22
|
-
unsupported_opts = opts.except(:tag_options, :url_params, :widths)
|
23
|
-
if unsupported_opts.length > 0
|
24
|
-
raise "'#{unsupported_opts.keys.join("', '")}' key(s) not supported; use tag_options, url_params, widths."
|
25
|
-
end
|
26
|
-
|
24
|
+
srcset_options = opts[:srcset_options] || {}
|
27
25
|
source_tag_opts[:media] ||= media
|
28
|
-
source_tag_opts[:srcset] ||= srcset(url_params: @url_params.clone.merge(source_tag_url_params),
|
26
|
+
source_tag_opts[:srcset] ||= srcset(url_params: @url_params.clone.merge(source_tag_url_params), srcset_options: srcset_options)
|
27
|
+
|
29
28
|
concat(content_tag(:source, nil, source_tag_opts))
|
30
29
|
end
|
31
30
|
|
32
|
-
concat Imgix::Rails::ImageTag.new(@path, source: @source, url_params: @url_params).render
|
31
|
+
concat Imgix::Rails::ImageTag.new(@path, source: @source, url_params: @url_params, srcset_options: @srcset_options).render
|
33
32
|
end
|
34
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def validate_opts(opts = {})
|
37
|
+
unsupported_opts = opts.except(:tag_options, :url_params, :srcset_options)
|
38
|
+
if unsupported_opts.length > 0
|
39
|
+
raise "'#{unsupported_opts.keys.join("', '")}' key(s) not supported; use tag_options, url_params, srcset_options."
|
40
|
+
end
|
41
|
+
end
|
35
42
|
end
|
data/lib/imgix/rails/tag.rb
CHANGED
@@ -1,83 +1,30 @@
|
|
1
1
|
require "imgix/rails/url_helper"
|
2
|
+
require "action_view"
|
2
3
|
|
3
4
|
class Imgix::Rails::Tag
|
4
5
|
include Imgix::Rails::UrlHelper
|
5
6
|
include ActionView::Helpers
|
6
7
|
|
7
|
-
def initialize(path, source: nil, tag_options: {}, url_params: {},
|
8
|
+
def initialize(path, source: nil, tag_options: {}, url_params: {}, srcset_options: {})
|
8
9
|
@path = path
|
9
10
|
@source = source
|
10
11
|
@tag_options = tag_options
|
11
12
|
@url_params = url_params
|
12
|
-
@
|
13
|
+
@srcset_options = srcset_options
|
13
14
|
end
|
14
15
|
|
15
16
|
protected
|
16
17
|
|
17
|
-
def srcset(url_params: @url_params,
|
18
|
-
|
19
|
-
warn "Warning: srcset generation will be refactored in the next major release. "\
|
20
|
-
"Please consult this project\'s documentation when upgrading to better understand the expected behavior."
|
21
|
-
end
|
22
|
-
widths = widths || target_widths
|
18
|
+
def srcset(source: @source, path: @path, url_params: @url_params, srcset_options: @srcset_options, tag_options: @tag_options)
|
19
|
+
params = url_params.clone
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
width_tolerance = ::Imgix::Rails.config.imgix[:srcset_width_tolerance]
|
22
|
+
min_width = @srcset_options[:min_width]
|
23
|
+
max_width = @srcset_options[:max_width]
|
24
|
+
widths = @srcset_options[:widths]
|
25
|
+
disable_variable_quality = @srcset_options[:disable_variable_quality]
|
26
|
+
options = { widths: widths, width_tolerance: width_tolerance, min_width: min_width, max_width: max_width, disable_variable_quality: disable_variable_quality}
|
27
27
|
|
28
|
-
|
29
|
-
srcset_url_params[:h] = (width * (url_params[:h].to_f / url_params[:w])).round
|
30
|
-
end
|
31
|
-
|
32
|
-
"#{ix_image_url(@source, @path, srcset_url_params)} #{width}w"
|
33
|
-
end.join(', ')
|
34
|
-
end
|
35
|
-
|
36
|
-
@@standard_widths = nil
|
37
|
-
|
38
|
-
def compute_standard_widths
|
39
|
-
tolerance = ::Imgix::Rails.config.imgix[:srcset_width_tolerance] || SRCSET_TOLERANCE
|
40
|
-
prev = MINIMUM_SCREEN_WIDTH
|
41
|
-
widths = []
|
42
|
-
while prev <= MAXIMUM_SCREEN_WIDTH do
|
43
|
-
widths.append(2 * (prev/2).round) # Ensure widths are even
|
44
|
-
prev = prev * (1 + tolerance*2.0)
|
45
|
-
end
|
46
|
-
|
47
|
-
widths
|
28
|
+
ix_image_srcset(@source, @path, params, options)
|
48
29
|
end
|
49
|
-
|
50
|
-
def standard_widths
|
51
|
-
return @@standard_widths if @@standard_widths
|
52
|
-
|
53
|
-
@@standard_widths = compute_standard_widths
|
54
|
-
@@standard_widths.freeze
|
55
|
-
|
56
|
-
@@standard_widths
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
MINIMUM_SCREEN_WIDTH = 100
|
62
|
-
MAXIMUM_SCREEN_WIDTH = 8192 # Maximum width supported by imgix
|
63
|
-
SRCSET_TOLERANCE = 0.08
|
64
|
-
|
65
|
-
# Return the widths to generate given the input `sizes`
|
66
|
-
# attribute.
|
67
|
-
#
|
68
|
-
# @return {Array} An array of {Fixnum} instances representing the unique `srcset` URLs to generate.
|
69
|
-
def target_widths
|
70
|
-
min_width = @tag_options[:min_width]
|
71
|
-
max_width = @tag_options[:max_width]
|
72
|
-
if min_width || max_width
|
73
|
-
min_width = min_width || MINIMUM_SCREEN_WIDTH
|
74
|
-
max_width = max_width || MAXIMUM_SCREEN_WIDTH
|
75
|
-
widths = standard_widths.select { |w| min_width <= w && w <= max_width }
|
76
|
-
else
|
77
|
-
widths = standard_widths
|
78
|
-
end
|
79
|
-
|
80
|
-
widths
|
81
|
-
end
|
82
|
-
|
83
30
|
end
|
@@ -10,28 +10,65 @@ module Imgix
|
|
10
10
|
when 1
|
11
11
|
path = args[0]
|
12
12
|
source = nil
|
13
|
-
|
13
|
+
params = {}
|
14
14
|
when 2
|
15
15
|
if args[0].is_a?(String) && args[1].is_a?(Hash)
|
16
16
|
source = nil
|
17
17
|
path = args[0]
|
18
|
-
|
18
|
+
params = args[1]
|
19
19
|
elsif args[0].is_a?(String) && args[1].is_a?(String)
|
20
20
|
source = args[0]
|
21
21
|
path = args[1]
|
22
|
-
|
22
|
+
params = {}
|
23
23
|
else
|
24
|
-
raise RuntimeError.new("path and source must be of type String;
|
24
|
+
raise RuntimeError.new("path and source must be of type String; params must be of type Hash")
|
25
25
|
end
|
26
26
|
when 3
|
27
27
|
source = args[0]
|
28
28
|
path = args[1]
|
29
|
-
|
29
|
+
params = args[2]
|
30
30
|
else
|
31
31
|
raise RuntimeError.new('path missing')
|
32
32
|
end
|
33
33
|
|
34
|
-
imgix_client(source).path(path).to_url(
|
34
|
+
imgix_client(source).path(path).to_url(params).html_safe
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def ix_image_srcset(*args)
|
40
|
+
validate_configuration!
|
41
|
+
|
42
|
+
case args.size
|
43
|
+
when 1
|
44
|
+
path = args[0]
|
45
|
+
source = nil
|
46
|
+
params = {}
|
47
|
+
when 2
|
48
|
+
if args[0].is_a?(String) && args[1].is_a?(Hash)
|
49
|
+
source = nil
|
50
|
+
path = args[0]
|
51
|
+
params = args[1]
|
52
|
+
elsif args[0].is_a?(String) && args[1].is_a?(String)
|
53
|
+
source = args[0]
|
54
|
+
path = args[1]
|
55
|
+
params = {}
|
56
|
+
else
|
57
|
+
raise RuntimeError.new("path and source must be of type String; params must be of type Hash")
|
58
|
+
end
|
59
|
+
when 3
|
60
|
+
source = args[0]
|
61
|
+
path = args[1]
|
62
|
+
params = args[2]
|
63
|
+
when 4
|
64
|
+
source = args[0]
|
65
|
+
path = args[1]
|
66
|
+
params = args[2]
|
67
|
+
options = args[3]
|
68
|
+
else
|
69
|
+
raise RuntimeError.new('path missing')
|
70
|
+
end
|
71
|
+
imgix_client(source).path(path).to_srcset(options: options, **params).html_safe
|
35
72
|
end
|
36
73
|
|
37
74
|
private
|
@@ -44,8 +81,8 @@ module Imgix
|
|
44
81
|
end
|
45
82
|
|
46
83
|
if imgix[:source]
|
47
|
-
unless imgix[:source].is_a?(
|
48
|
-
raise ConfigurationError.new("imgix source must be a String
|
84
|
+
unless imgix[:source].is_a?(String)
|
85
|
+
raise ConfigurationError.new("imgix source must be a String.")
|
49
86
|
end
|
50
87
|
end
|
51
88
|
|
@@ -54,10 +91,6 @@ module Imgix
|
|
54
91
|
raise ConfigurationError.new(":sources must be a Hash")
|
55
92
|
end
|
56
93
|
end
|
57
|
-
|
58
|
-
unless !imgix.key?(:shard_strategy) || STRATEGIES.include?(imgix[:shard_strategy])
|
59
|
-
raise ConfigurationError.new("#{imgix[:shard_strategy]} is not supported")
|
60
|
-
end
|
61
94
|
end
|
62
95
|
|
63
96
|
def imgix_client(source)
|
@@ -80,8 +113,6 @@ module Imgix
|
|
80
113
|
|
81
114
|
if imgix[:source].is_a?(String)
|
82
115
|
opts[:host] = imgix[:source]
|
83
|
-
else
|
84
|
-
opts[:hosts] = imgix[:source]
|
85
116
|
end
|
86
117
|
|
87
118
|
if imgix.has_key?(:include_library_param)
|
@@ -92,10 +123,6 @@ module Imgix
|
|
92
123
|
opts[:use_https] = imgix[:use_https]
|
93
124
|
end
|
94
125
|
|
95
|
-
if imgix.has_key?(:shard_strategy)
|
96
|
-
opts[:shard_strategy] = imgix[:shard_strategy]
|
97
|
-
end
|
98
|
-
|
99
126
|
sources = imgix[:sources] || { imgix[:source] => imgix[:secure_url_token] }
|
100
127
|
@imgix_clients = {}
|
101
128
|
|
data/lib/imgix/rails/version.rb
CHANGED
@@ -8,12 +8,12 @@ module Imgix
|
|
8
8
|
module ViewHelper
|
9
9
|
include UrlHelper
|
10
10
|
|
11
|
-
def ix_image_tag(source=nil, path, tag_options: {}, url_params: {},
|
12
|
-
return Imgix::Rails::ImageTag.new(path, source: source, tag_options: tag_options, url_params: url_params,
|
11
|
+
def ix_image_tag(source=nil, path, tag_options: {}, url_params: {}, srcset_options: {})
|
12
|
+
return Imgix::Rails::ImageTag.new(path, source: source, tag_options: tag_options, url_params: url_params, srcset_options: srcset_options).render
|
13
13
|
end
|
14
14
|
|
15
|
-
def ix_picture_tag(source=nil, path, tag_options: {}, url_params: {}, breakpoints: {},
|
16
|
-
return Imgix::Rails::PictureTag.new(path, source: source, tag_options: tag_options, url_params: url_params, breakpoints: breakpoints,
|
15
|
+
def ix_picture_tag(source=nil, path, tag_options: {}, url_params: {}, breakpoints: {}, srcset_options: {})
|
16
|
+
return Imgix::Rails::PictureTag.new(path, source: source, tag_options: tag_options, url_params: url_params, breakpoints: breakpoints, srcset_options: srcset_options).render
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imgix-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Sutton
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: imgix
|
@@ -17,20 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 1.1.0
|
20
|
+
version: '3.0'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - "~>"
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: '
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 1.1.0
|
27
|
+
version: '3.0'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: bundler
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -120,6 +114,10 @@ homepage: https://github.com/imgix/imgix-rails
|
|
120
114
|
licenses:
|
121
115
|
- BSD-2-Clause
|
122
116
|
metadata:
|
117
|
+
bug_tracker_uri: https://github.com/imgix/imgix-rails/issues
|
118
|
+
changelog_uri: https://github.com/imgix/imgix-rails/blob/master/CHANGELOG.md
|
119
|
+
documentation_uri: https://www.rubydoc.info/gems/imgix-rails/4.0.0
|
120
|
+
source_code_uri: https://github.com/imgix/imgix-rails/tree/v4.0.0
|
123
121
|
allowed_push_host: https://rubygems.org
|
124
122
|
post_install_message:
|
125
123
|
rdoc_options: []
|