Skip to content

Commit 0b2dda2

Browse files
committed
Add info options support
1 parent c4d1d16 commit 0b2dda2

32 files changed

Lines changed: 1313 additions & 555 deletions

README.md

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ production:
7575
- With environment variables:
7676

7777
```bash
78-
IMGPROXY_ENDPOINT="http://imgproxy.example.com"\
79-
IMGPROXY_KEY="your_key"\
80-
IMGPROXY_SALT="your_salt"\
78+
IMGPROXY_ENDPOINT="http://imgproxy.example.com" \
79+
IMGPROXY_KEY="your_key" \
80+
IMGPROXY_SALT="your_salt" \
8181
rails s
8282
```
8383

@@ -95,7 +95,7 @@ Imgproxy.configure do |config|
9595
end
9696
```
9797

98-
#### Configuration options
98+
### Configuration options
9999

100100
- **endpoint** (`IMGPROXY_ENDPOINT`) - Full URL to your imgproxy instance. Default: `nil`.
101101
- **key** (`IMGPROXY_KEY`) - Hex-encoded signature key. Default: `nil`.
@@ -129,10 +129,18 @@ Imgproxy.extend_active_storage!
129129
Now, to add imgproxy processing to your image attachments, just use the `imgproxy_url` method:
130130

131131
```ruby
132-
user.avatar.imgproxy_url(width: 250, height: 250)
132+
user.avatar.imgproxy_url(width: 500, height: 400, resizing_type: :fill)
133133
```
134134

135-
This method will return an URL to your user's avatar, resized to 250x250px on the fly.
135+
This method will return a URL to your user's avatar, resized to fill 500x400px on the fly.
136+
137+
If you're a happy user of [imgproxy Pro](https://imgproxy.net#pro), you may find useful it's [Getting an image info](https://docs.imgproxy.net/usage/getting_info) feature. imgproxy.rb allows you to easily generate info URLs for your images:
138+
139+
```ruby
140+
user.avatar.imgproxy_info_url(detect_objects: true, palette: 128)
141+
```
142+
143+
This method will return a URL to the JSON with the requested info about your user's avatar.
136144

137145
#### Amazon S3
138146

@@ -157,10 +165,18 @@ Imgproxy.extend_shrine!
157165
Now you can use `imgproxy_url` method of `Shrine::UploadedFile`:
158166

159167
```ruby
160-
user.avatar.imgproxy_url(width: 250, height: 250)
168+
user.avatar.imgproxy_url(width: 500, height: 400, resizing_type: :fill)
161169
```
162170

163-
This method will return an URL to your user's avatar, resized to 250x250px on the fly.
171+
This method will return a URL to your user's avatar, resized to fill 500x400px on the fly.
172+
173+
If you're a happy user of [imgproxy Pro](https://imgproxy.net#pro), you may find useful it's [Getting an image info](https://docs.imgproxy.net/usage/getting_info) feature. imgproxy.rb allows you to easily generate info URLs for your images:
174+
175+
```ruby
176+
user.avatar.imgproxy_info_url(detect_objects: true, palette: 128)
177+
```
178+
179+
This method will return a URL to the JSON with the requested info about your user's avatar.
164180

165181
**NOTE:** If you use `Shrine::Storage::FileSystem` as storage, uploaded file URLs won't include the hostname, so imgproxy server won't be able to access them. To fix this, use `shrine_host` config.
166182

@@ -183,16 +199,28 @@ Imgproxy.url_for(
183199
"http://images.example.com/images/image.jpg",
184200
width: 500,
185201
height: 400,
186-
resizing_type: :fill,
187-
sharpen: 0.5
202+
resizing_type: :fill
203+
)
204+
```
205+
206+
This method will return a URL to the image, resized to fill 500x400px on the fly.
207+
208+
If you're a happy user of [imgproxy Pro](https://imgproxy.net#pro), you may find useful it's [Getting an image info](https://docs.imgproxy.net/usage/getting_info) feature. imgproxy.rb allows you to easily generate info URLs for your images:
209+
210+
```ruby
211+
Imgproxy.info_url_for(
212+
"http://images.example.com/images/image.jpg",
213+
detect_objects: true,
214+
palette: 128
188215
)
189-
# => http://imgproxy.example.com/2tjGMpWqjO/rs:fill:500:400/sh:0.5/plain/http://images.example.com/images/image.jpg
190216
```
191217

218+
This method will return a URL to the JSON with the requested info about the image.
219+
192220
You can reuse processing options by using `Imgproxy::Builder`:
193221

194222
```ruby
195-
builder = Imgproxy::Builder.new(
223+
builder = Imgproxy::UrlBuilders::Processing.new(
196224
width: 500,
197225
height: 400,
198226
resizing_type: :fill,
@@ -201,9 +229,27 @@ builder = Imgproxy::Builder.new(
201229
202230
builder.url_for("http://images.example.com/images/image1.jpg")
203231
builder.url_for("http://images.example.com/images/image2.jpg")
232+
233+
info_builder = Imgproxy::UrlBuilders::Info.new(
234+
detect_objects: true,
235+
palette: 128
236+
)
237+
238+
info_builder.url_for("http://images.example.com/images/image1.jpg")
239+
info_builder.url_for("http://images.example.com/images/image2.jpg")
204240
```
205241

206-
### Supported imgproxy processing options
242+
## Supported imgproxy options
243+
244+
### Common options
245+
246+
- `base64_encode_url` — per-call redefinition of `base64_encode_urls` config.
247+
- `escape_plain_url` — per-call redefinition of `always_escape_plain_urls` config.
248+
- `use_short_options` — per-call redefinition of `use_short_options` config.
249+
- `encrypt_source_url` - _(pro)_ per-call redefinition of `always_encrypt_source_urls` config.
250+
- `source_url_encryption_iv` - _(pro)_ an initialization vector (IV) to be used for the source URL encryption if encryption is needed. If not specified, a random IV is used.
251+
252+
### Processing options
207253

208254
- [resize](https://docs.imgproxy.net/usage/processing#resize)
209255
- [size](https://docs.imgproxy.net/usage/processing#size)
@@ -250,11 +296,41 @@ builder.url_for("http://images.example.com/images/image2.jpg")
250296
- [return_attachment](https://docs.imgproxy.net/usage/processing#return-attachment)
251297
- [expires](https://docs.imgproxy.net/usage/processing#expires)
252298

253-
_See [imgproxy URL format guide](https://docs.imgproxy.net/usage/processing#processing-options) for more info._
299+
_See [imgproxy processing options guide](https://docs.imgproxy.net/usage/processing#processing-options) for more info._
300+
301+
### Info options (pro)
302+
303+
- [size](https://docs.imgproxy.net/usage/getting_info#size)
304+
- [format](https://docs.imgproxy.net/usage/getting_info#format)
305+
- [dimensions](https://docs.imgproxy.net/usage/getting_info#dimensions)
306+
- [video_meta](https://docs.imgproxy.net/usage/getting_info#video_meta)
307+
- [detect_objects](https://docs.imgproxy.net/usage/getting_info#detect_objects)
308+
- [colorspace](https://docs.imgproxy.net/usage/getting_info#colorspace)
309+
- [bands](https://docs.imgproxy.net/usage/getting_info#bands)
310+
- [sample_format](https://docs.imgproxy.net/usage/getting_info#sample_format)
311+
- [pages_number](https://docs.imgproxy.net/usage/getting_info#pages_number)
312+
- [alpha](https://docs.imgproxy.net/usage/getting_info#alpha)
313+
- [crop](https://docs.imgproxy.net/usage/getting_info#crop)
314+
- [palette](https://docs.imgproxy.net/usage/getting_info#palette)
315+
- [average](https://docs.imgproxy.net/usage/getting_info#average)
316+
- [dominant_colors](https://docs.imgproxy.net/usage/getting_info#dominant_colors)
317+
- [blurhash](https://docs.imgproxy.net/usage/getting_info#blurhash)
318+
- [calc_hashsum](https://docs.imgproxy.net/usage/getting_info#calc_hashsum)
319+
- [page](https://docs.imgproxy.net/usage/getting_info#page)
320+
- [video_thumbnail_second](https://docs.imgproxy.net/usage/getting_info#video_thumbnail_second)
321+
- [video_thumbnail_keyframes](https://docs.imgproxy.net/usage/getting_info#video_thumbnail_keyframes)
322+
- [cachebuster](https://docs.imgproxy.net/usage/getting_info#cachebuster)
323+
- [expires](https://docs.imgproxy.net/usage/getting_info#expires)
324+
- [preset](https://docs.imgproxy.net/usage/getting_info#preset)
325+
- [hashsum](https://docs.imgproxy.net/usage/getting_info#hashsum)
326+
- [max_src_resolution](https://docs.imgproxy.net/usage/getting_info#max_src_resolution)
327+
- [max_src_file_size](https://docs.imgproxy.net/usage/getting_info#max_src_file_size)
328+
329+
_See [imgproxy info options guide](https://docs.imgproxy.net/usage/getting_info#info-options) for more info._
254330

255331
### Complex processing options
256332

257-
Some of the processing options like `crop` or `gravity` may have multiple arguments, and you can define these arguments multiple ways:
333+
Some of the processing and info options like `crop` or `gravity` may have multiple arguments, and you can define these arguments multiple ways:
258334

259335
#### Named arguments
260336

@@ -335,35 +411,6 @@ Imgproxy.url_for(
335411
# => .../wmu:aHR0cDovL2V4YW1wbGUuY29tL3dhdGVybWFyay5qcGc/st:Y29sb3I6IHJnYmEoMjU1LCAyNTUsIDI1NSwgLjUp/...
336412
```
337413

338-
### Special options:
339-
340-
- `base64_encode_url` — per-call redefinition of `base64_encode_urls` config.
341-
- `escape_plain_url` — per-call redefinition of `always_escape_plain_urls` config.
342-
- `use_short_options` — per-call redefinition of `use_short_options` config.
343-
- `encrypt_source_url` - _(pro)_ per-call redefinition of `always_encrypt_source_urls` config.
344-
- `source_url_encryption_iv` - _(pro)_ an initialization vector (IV) to be used for the source URL encryption if encryption is needed. If not specified, a random IV is used.
345-
346-
## Getting the image info
347-
348-
If you're a happy user of imgproxy Pro, you may find useful it's [Getting the image info](https://docs.imgproxy.net/usage/getting_info) feature. imgproxy.rb allows you to easily generate info URLs for your images:
349-
350-
```ruby
351-
# Framework-agnositic way
352-
Imgproxy.info_url_for("http://images.example.com/images/image.jpg")
353-
# Using Active Storage or Shrine
354-
user.avatar.imgproxy_info_url
355-
356-
# You can also use base64_encode_url or escape_plain_url options
357-
Imgproxy.info_url_for(
358-
"http://images.example.com/images/image.jpg",
359-
base64_encode_url: true
360-
)
361-
Imgproxy.info_url_for(
362-
"http://images.example.com/images/image.jpg",
363-
escape_plain_url: true
364-
)
365-
```
366-
367414
## URL adapters
368415

369416
By default, `Imgproxy.url_for` accepts only `String` and `URI` as the source URL, but you can extend that behavior by using URL adapters.

lib/imgproxy.rb

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "imgproxy/version"
22
require "imgproxy/config"
3-
require "imgproxy/builder"
3+
require "imgproxy/url_builders/processing"
4+
require "imgproxy/url_builders/info"
45

56
require "imgproxy/extensions/active_storage"
67
require "imgproxy/extensions/shrine"
@@ -59,73 +60,31 @@ def configure
5960
# @param [String,URI, Object] image Source image URL or object applicable for
6061
# the configured URL adapters
6162
# @param [Hash] options Processing options
62-
# @option options [Hash|Array|String] :resize
63-
# @option options [Hash|Array|String] :size
64-
# @option options [String] :resizing_type
65-
# @option options [String] :resizing_algorithm supported only by imgproxy pro
66-
# @option options [Integer] :width
67-
# @option options [Integer] :height
68-
# @option options [Float] :dpr
69-
# @option options [Boolean] :enlarge
70-
# @option options [Hash|Array|Boolean|String] :extend
71-
# @option options [Hash|Array|String] :gravity
72-
# @option options [Hash|Array|String] :crop
73-
# @option options [Array] :padding
74-
# @option options [Hash|Array|String] :trim
75-
# @option options [Integer] :rotate
76-
# @option options [Integer] :quality
77-
# @option options [Integer] :max_bytes
78-
# @option options [Array|String] :background
79-
# @option options [Float] :background_alpha supported only by imgproxy pro
80-
# @option options [Hash|Array|String] :adjust
81-
# @option options [Integer] :brightness supported only by imgproxy pro
82-
# @option options [Float] :contrast supported only by imgproxy pro
83-
# @option options [Float] :saturation supported only by imgproxy pro
84-
# @option options [Float] :blur
85-
# @option options [Float] :sharpen
86-
# @option options [Integer] :pixelate supported only by imgproxy pro
87-
# @option options [String] :unsharpening supported only by imgproxy pro
88-
# @option options [Hash|Array|Float|String] :watermark
89-
# @option options [String] :watermark_url supported only by imgproxy pro
90-
# @option options [String] :watermark_text supported only by imgproxy pro
91-
# @option options [String] :style supported only by imgproxy pro
92-
# @option options [Hash|Array|String] :jpeg_options supported only by imgproxy pro
93-
# @option options [Hash|Array|String] :png_options supported only by imgproxy pro
94-
# @option options [Hash|Array|String] :gif_options supported only by imgproxy pro
95-
# @option options [Integer] :page supported only by imgproxy pro
96-
# @option options [Integer] :video_thumbnail_second supported only by imgproxy pro
97-
# @option options [Array] :preset
98-
# @option options [String] :cachebuster
99-
# @option options [Boolean] :strip_metadata
100-
# @option options [Boolean] :strip_color_profile
101-
# @option options [Boolean] :auto_rotate
102-
# @option options [String] :filename
103-
# @option options [String] :format
104-
# @option options [Boolean] :return_attachment
105-
# @option options [Integer] :expires
106-
# @option options [Boolean] :use_short_options
107-
# @option options [Boolean] :base64_encode_urls
108-
# @option options [Boolean] :escape_plain_url
109-
# @option options [Boolean] :encrypt_source_url
110-
# @option options [Boolean] :source_url_encryption_iv
111-
# @see https://docs.imgproxy.net/generating_the_url_advanced?id=processing-options
112-
# Available imgproxy URL processing options and their arguments
63+
# @see https://github.com/imgproxy/imgproxy.rb#processing-options
64+
# Supported processing options
11365
def url_for(image, options = {})
114-
Imgproxy::Builder.new(options).url_for(image)
66+
Imgproxy::UrlBuilders::Processing.new(options).url_for(image)
11567
end
11668

117-
# Genrates imgproxy info URL. Supported only by imgproxy pro
69+
# Genrates imgproxy info URL. Supported only by imgproxy Pro
11870
#
119-
# Imgproxy.info_url_for("http://images.example.com/images/image.jpg")
71+
# Imgproxy.info_url_for(
72+
# "http://images.example.com/images/image.jpg",
73+
# alpha: {
74+
# alpha: true,
75+
# check_transparency: true
76+
# },
77+
# palette: 128,
78+
# )
12079
#
12180
# @return [String] imgproxy info URL
12281
# @param [String,URI, Object] image Source image URL or object applicable for
12382
# the configured URL adapters
124-
# @param [Hash] options Processing options
125-
# @option options [Boolean] :base64_encode_urls
126-
# @option options [Boolean] :escape_plain_url
83+
# @param [Hash] options Info options
84+
# @see https://github.com/imgproxy/imgproxy.rb#info-options
85+
# Supported info options
12786
def info_url_for(image, options = {})
128-
Imgproxy::Builder.new(options).info_url_for(image)
87+
Imgproxy::UrlBuilders::Info.new(options).url_for(image)
12988
end
13089

13190
# Extends +ActiveStorage::Blob+ with {Imgproxy::Extensions::ActiveStorage.imgproxy_url} method

0 commit comments

Comments
 (0)