imgproxy 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +235 -112
- data/lib/imgproxy.rb +70 -50
- data/lib/imgproxy/builder.rb +47 -55
- data/lib/imgproxy/config.rb +96 -30
- data/lib/imgproxy/extensions/active_storage.rb +10 -0
- data/lib/imgproxy/extensions/shrine.rb +10 -0
- data/lib/imgproxy/options.rb +83 -121
- data/lib/imgproxy/options_aliases.rb +43 -0
- data/lib/imgproxy/options_casters/adjust.rb +22 -0
- data/lib/imgproxy/options_casters/array.rb +12 -0
- data/lib/imgproxy/options_casters/base64.rb +12 -0
- data/lib/imgproxy/options_casters/bool.rb +12 -0
- data/lib/imgproxy/options_casters/crop.rb +23 -0
- data/lib/imgproxy/options_casters/extend.rb +26 -0
- data/lib/imgproxy/options_casters/float.rb +16 -0
- data/lib/imgproxy/options_casters/gif_options.rb +21 -0
- data/lib/imgproxy/options_casters/gravity.rb +23 -0
- data/lib/imgproxy/options_casters/group.rb +21 -0
- data/lib/imgproxy/options_casters/integer.rb +10 -0
- data/lib/imgproxy/options_casters/jpeg_options.rb +26 -0
- data/lib/imgproxy/options_casters/png_options.rb +23 -0
- data/lib/imgproxy/options_casters/resize.rb +21 -0
- data/lib/imgproxy/options_casters/size.rb +24 -0
- data/lib/imgproxy/options_casters/string.rb +10 -0
- data/lib/imgproxy/options_casters/trim.rb +28 -0
- data/lib/imgproxy/options_casters/watermark.rb +30 -0
- data/lib/imgproxy/trim_array.rb +11 -0
- data/lib/imgproxy/url_adapters.rb +0 -4
- data/lib/imgproxy/url_adapters/active_storage.rb +25 -0
- data/lib/imgproxy/url_adapters/shrine.rb +15 -5
- data/lib/imgproxy/version.rb +1 -1
- metadata +54 -9
- data/lib/imgproxy/url_adapters/active_storage_gcs.rb +0 -31
- data/lib/imgproxy/url_adapters/active_storage_s3.rb +0 -23
- data/lib/imgproxy/url_adapters/shrine_s3.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55e93cacc2a68c479e9b76cfadbd45cbe6e0dabc3c12628de3eeef0519f4839b
|
4
|
+
data.tar.gz: e0ac82d77ea5ac64fbe47ed895aea1f48bf01de610bece8f344a6afb9c84c2e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf8141488c246b00affdb21d533987c7af1345e4fda278f5a08dcc53e2e7e47829f0ce0101502a125c76933a651f4fd9ad45721e08aac5832f6d6c08740c7732
|
7
|
+
data.tar.gz: cdf4c6d1d7173d75c6fd11c56c6ebb5242a557071d8e78280ec9b9f451e6ed2ffda5f896b7d4fb2f4fbcc6f4df0e50589b19b7be7cd3e801fc580ea061f1e83d
|
data/README.md
CHANGED
@@ -7,11 +7,11 @@
|
|
7
7
|
|
8
8
|
**[imgproxy](https://github.com/imgproxy/imgproxy)** is a fast and secure standalone server for resizing and converting remote images. The main principles of imgproxy are simplicity, speed, and security. It is a Go application, ready to be installed and used in any Unix environment—also ready to be containerized using Docker.
|
9
9
|
|
10
|
-
imgproxy can be used to provide a fast and secure way to
|
10
|
+
imgproxy can be used to provide a fast and secure way to _get rid of all the image resizing code_ in your web application (like calling ImageMagick or GraphicsMagick, or using libraries), while also being able to resize everything on the fly on a separate server that only you control. imgproxy is fast, easy to use, and requires zero processing power or storage from the main application. imgproxy is indispensable when handling image resizing of epic proportions, especially when original images are coming from a remote source.
|
11
11
|
|
12
|
-
|
12
|
+
[imgproxy.rb](https://github.com/imgproxy/imgproxy.rb) is a framework-agnostic Ruby Gem for imgproxy that includes proper support for Ruby on Rails' most popular image attachment options: [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) and [Shrine](https://github.com/shrinerb/shrine).
|
13
13
|
|
14
|
-
|
14
|
+
**NOTE:** this readme shows documentation for 2.x version. For version 1.x see the [v1.2.0](https://github.com/imgproxy/imgproxy.rb/tree/v1.2.0) tag. See [2.0-Upgrade.md](2.0-Upgrade.md) for the upgrade guide.
|
15
15
|
|
16
16
|
<a href="https://evilmartians.com/?utm_source=imgproxy.rb">
|
17
17
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
@@ -33,35 +33,81 @@ gem install imgproxy
|
|
33
33
|
|
34
34
|
## Configuration
|
35
35
|
|
36
|
-
|
36
|
+
imgproxy.rb uses [anyway_config](https://github.com/palkan/anyway_config) to load configuration, so you can configure it in different ways.
|
37
|
+
|
38
|
+
- With a separate config file:
|
39
|
+
|
40
|
+
```yaml
|
41
|
+
# <Rails root>/config/imgproxy.yml
|
42
|
+
development:
|
43
|
+
# Full URL to where your imgproxy lives.
|
44
|
+
endpoint: "http://imgproxy.example.com"
|
45
|
+
# Hex-encoded signature key and salt
|
46
|
+
key: "your_key"
|
47
|
+
salt: "your_salt"
|
48
|
+
production: ...
|
49
|
+
test: ...
|
50
|
+
```
|
51
|
+
|
52
|
+
- With a `secrets.yml` entry for imgproxy:
|
53
|
+
|
54
|
+
```yaml
|
55
|
+
# secrets.yml
|
56
|
+
production:
|
57
|
+
...
|
58
|
+
imgproxy:
|
59
|
+
# Full URL to where your imgproxy lives.
|
60
|
+
endpoint: "http://imgproxy.example.com"
|
61
|
+
# Hex-encoded signature key and salt
|
62
|
+
key: "your_key"
|
63
|
+
salt: "your_salt"
|
64
|
+
...
|
65
|
+
```
|
66
|
+
|
67
|
+
- With environment variables:
|
68
|
+
|
69
|
+
```bash
|
70
|
+
IMGPROXY_ENDPOINT="http://imgproxy.example.com"\
|
71
|
+
IMGPROXY_KEY="your_key"\
|
72
|
+
IMGPROXY_SALT="your_salt"\
|
73
|
+
rails s
|
74
|
+
```
|
75
|
+
|
76
|
+
- ...or right in your application code:
|
37
77
|
|
38
78
|
```ruby
|
39
79
|
# config/initializers/imgproxy.rb
|
40
80
|
|
41
81
|
Imgproxy.configure do |config|
|
42
|
-
# imgproxy endpoint
|
43
|
-
#
|
44
82
|
# Full URL to where your imgproxy lives.
|
45
83
|
config.endpoint = "http://imgproxy.example.com"
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# Hex-encoded signature key
|
51
|
-
config.hex_key = "your_key"
|
52
|
-
# Hex-encoded signature salt
|
53
|
-
config.hex_salt = "your_salt"
|
54
|
-
|
55
|
-
# Base64 encode all URLs
|
56
|
-
# config.base64_encode_urls = true
|
84
|
+
# Hex-encoded signature key and salt
|
85
|
+
config.key = "your_key"
|
86
|
+
config.salt = "your_salt"
|
57
87
|
end
|
58
88
|
```
|
59
89
|
|
90
|
+
#### Configuration options
|
91
|
+
|
92
|
+
- **endpoint** (`IMGPROXY_ENDPOINT`) - Full URL to your imgproxy instance. Default: `nil`.
|
93
|
+
- **key** (`IMGPROXY_KEY`) - Hex-encoded signature key. Default: `nil`.
|
94
|
+
- **salt** (`IMGPROXY_SALT`) - Hex-encoded signature salt. Default: `nil`.
|
95
|
+
- **raw_key** (`IMGPROXY_RAW_KEY`) - Raw (not hex-encoded) signature key. Default: `nil`.
|
96
|
+
- **raw_salt** (`IMGPROXY_RAW_SALT`) - Raw (not hex-encoded) signature salt. Default: `nil`.
|
97
|
+
- **signature_size** (`IMGPROXY_SIGNATURE_SIZE`) - Signature size. See [URL signature](https://docs.imgproxy.net/#/configuration?id=url-signature) section of imgproxy docs. Default: 32.
|
98
|
+
- **use_short_options** (`IMGPROXY_USE_SHORT_OPTIONS`) - Use short processing options names (`rs` for `resize`, `g` for `gravity`, etc). Default: true.
|
99
|
+
- **base64_encode_urls** (`IMGPROXY_BASE64_ENCODE_URLS`) - Encode source URLs to base64. Default: false.
|
100
|
+
- **always_escape_plain_urls** (`IMGPROXY_ALWAYS_ESCAPE_PLAIN_URLS`) - Always escape plain source URLs even when ones don't need to be escaped. Default: false.
|
101
|
+
- **use_s3_urls** (`IMGPROXY_USE_S3_URLS`) - Use `s3://...` source URLs for Active Storage and Shrine attachments stored in Amazon S3. Default: false.
|
102
|
+
- **use_gcs_urls** (`IMGPROXY_USE_GCS_URLS`) - Use `gs://...` source URLs for Active Storage and Shrine attachments stored in Google Cloud Storage. Default: false.
|
103
|
+
- **gcs_bucket** (`IMGPROXY_GCS_BUCKET`) - Google Cloud Storage bucket name. Default: `nil`.
|
104
|
+
- **shrine_host** (`IMGPROXY_SHRINE_HOST`) - Shrine host for locally stored files.
|
105
|
+
|
60
106
|
## Usage
|
61
107
|
|
62
108
|
### Using with Active Storage
|
63
109
|
|
64
|
-
imgproxy.rb comes with
|
110
|
+
imgproxy.rb comes with the Active Storage support built-in. It is enabled _automagically_ if you load `imgproxy` gem after `rails` (basically, just put `gem "imgproxy"` after `gem "rails"` in your `Gemfile`). Otherwise, modify your initializer at `config/initializers/imgproxy.rb`:
|
65
111
|
|
66
112
|
```ruby
|
67
113
|
# config/initializers/imgproxy.rb
|
@@ -75,29 +121,21 @@ Now, to add imgproxy processing to your image attachments, just use the `imgprox
|
|
75
121
|
user.avatar.imgproxy_url(width: 250, height: 250)
|
76
122
|
```
|
77
123
|
|
78
|
-
will
|
79
|
-
|
80
|
-
If you have configured both your imgproxy server and Active Storage to work with Amazon S3, you may want to use the short `s3://...` source URL scheme instead of the long one generated by Rails:
|
81
|
-
|
82
|
-
```ruby
|
83
|
-
# config/initializers/imgproxy.rb
|
124
|
+
This method will return an URL to your user's avatar, resized to 250x250px on the fly.
|
84
125
|
|
85
|
-
|
86
|
-
```
|
126
|
+
#### Amazon S3
|
87
127
|
|
88
|
-
|
128
|
+
If you have configured both your imgproxy server and Active Storage to work with Amazon S3, you can use `use_s3_urls` config option (or `IMGPROXY_USE_S3_URLS` env variable) to make imgproxy.rb use short `s3://...` source URLs instead of long ones generated by Rails.
|
89
129
|
|
90
|
-
|
91
|
-
# config/initializers/imgproxy.rb
|
130
|
+
#### Google Cloud Storage
|
92
131
|
|
93
|
-
|
94
|
-
```
|
132
|
+
You can also enable `gs://...` URLs usage for the files stored in Google Cloud Storage with `use_gcs_urls` and `gcs_bucket` config options (or `IMGPROXY_USE_GCS_URLS` and `IMGPROXY_GCS_BUCKET` env variables).
|
95
133
|
|
96
|
-
**
|
134
|
+
**NOTE** that you need to explicitly provide GCS bucket name since Active Storage "hides" the GCS config.
|
97
135
|
|
98
136
|
### Using with Shrine
|
99
137
|
|
100
|
-
You can also use imgproxy.rb's built-in [Shrine](https://github.com/shrinerb/shrine) support.
|
138
|
+
You can also use imgproxy.rb's built-in [Shrine](https://github.com/shrinerb/shrine) support. It is enabled automagically if you load `imgproxy` gem after `shrine` (basically, just put `gem "imgproxy"` after `gem "shrine"` in your `Gemfile`). Otherwise, modify your initializer at `config/initializers/imgproxy.rb`:
|
101
139
|
|
102
140
|
```ruby
|
103
141
|
# config/initializers/imgproxy.rb
|
@@ -111,15 +149,9 @@ Now you can use `imgproxy_url` method of `Shrine::UploadedFile`:
|
|
111
149
|
user.avatar.imgproxy_url(width: 250, height: 250)
|
112
150
|
```
|
113
151
|
|
114
|
-
will
|
115
|
-
|
116
|
-
**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 `host` option of `Imgproxy.extend_shrine!`:
|
117
|
-
|
118
|
-
```ruby
|
119
|
-
# config/initializers/imgproxy.rb
|
152
|
+
This method will return an URL to your user's avatar, resized to 250x250px on the fly.
|
120
153
|
|
121
|
-
|
122
|
-
```
|
154
|
+
**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.
|
123
155
|
|
124
156
|
Alternatively, you can launch your imgproxy server with the `IMGPROXY_BASE_URL` setting:
|
125
157
|
|
@@ -127,13 +159,11 @@ Alternatively, you can launch your imgproxy server with the `IMGPROXY_BASE_URL`
|
|
127
159
|
IMGPROXY_BASE_URL="http://your-host.test" imgproxy
|
128
160
|
```
|
129
161
|
|
130
|
-
|
162
|
+
#### Amazon S3
|
131
163
|
|
132
|
-
|
133
|
-
Imgproxy.extend_shrine!(use_s3: true)
|
134
|
-
```
|
164
|
+
If you have configured both your imgproxy server and Shrine to work with Amazon S3, you can use `use_s3_urls` config option (or `IMGPROXY_USE_S3_URLS` env variable) to make imgproxy.rb use short `s3://...` source URLs instead of long ones generated by Shrine.
|
135
165
|
|
136
|
-
### Usage in a framework-agnostic way
|
166
|
+
### Usage imgproxy.rb in a framework-agnostic way
|
137
167
|
|
138
168
|
If you use another gem for your attachment operations, you like to keep things minimal or Rails-free, or if you want to generate imgproxy URLs for pictures that did not originate from your application, you can use the `Imgproxy.url_for` method:
|
139
169
|
|
@@ -162,44 +192,163 @@ builder.url_for("http://images.example.com/images/image1.jpg")
|
|
162
192
|
builder.url_for("http://images.example.com/images/image2.jpg")
|
163
193
|
```
|
164
194
|
|
165
|
-
###
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
195
|
+
### Supported imgproxy processing options
|
196
|
+
|
197
|
+
- [resize](https://docs.imgproxy.net/#/generating_the_url_advanced?id=resize)
|
198
|
+
- [size](https://docs.imgproxy.net/#/generating_the_url_advanced?id=size)
|
199
|
+
- [resizing_type](https://docs.imgproxy.net/#/generating_the_url_advanced?id=resizing-type)
|
200
|
+
- [resizing_algorithm](https://docs.imgproxy.net/#/generating_the_url_advanced?id=resizing-algorithm) _(pro)_
|
201
|
+
- [width](https://docs.imgproxy.net/#/generating_the_url_advanced?id=width)
|
202
|
+
- [height](https://docs.imgproxy.net/#/generating_the_url_advanced?id=height)
|
203
|
+
- [dpr](https://docs.imgproxy.net/#/generating_the_url_advanced?id=dpr)
|
204
|
+
- [enlarge](https://docs.imgproxy.net/#/generating_the_url_advanced?id=enlarge)
|
205
|
+
- [extend](https://docs.imgproxy.net/#/generating_the_url_advanced?id=extend)
|
206
|
+
- [gravity](https://docs.imgproxy.net/#/generating_the_url_advanced?id=gravity)
|
207
|
+
- [crop](https://docs.imgproxy.net/#/generating_the_url_advanced?id=crop)
|
208
|
+
- [padding](https://docs.imgproxy.net/#/generating_the_url_advanced?id=padding)
|
209
|
+
- [trim](https://docs.imgproxy.net/#/generating_the_url_advanced?id=trim)
|
210
|
+
- [rotate](https://docs.imgproxy.net/#/generating_the_url_advanced?id=rotate)
|
211
|
+
- [quality](https://docs.imgproxy.net/#/generating_the_url_advanced?id=quality)
|
212
|
+
- [max_bytes](https://docs.imgproxy.net/#/generating_the_url_advanced?id=max-bytes)
|
213
|
+
- [background](https://docs.imgproxy.net/#/generating_the_url_advanced?id=background)
|
214
|
+
- [background_alpha](https://docs.imgproxy.net/#/generating_the_url_advanced?id=background-alpha) _(pro)_
|
215
|
+
- [adjust](https://docs.imgproxy.net/#/generating_the_url_advanced?id=adjust) _(pro)_
|
216
|
+
- [brightness](https://docs.imgproxy.net/#/generating_the_url_advanced?id=brightness) _(pro)_
|
217
|
+
- [contrast](https://docs.imgproxy.net/#/generating_the_url_advanced?id=contrast) _(pro)_
|
218
|
+
- [saturation](https://docs.imgproxy.net/#/generating_the_url_advanced?id=saturation) _(pro)_
|
219
|
+
- [blur](https://docs.imgproxy.net/#/generating_the_url_advanced?id=blur)
|
220
|
+
- [sharpen](https://docs.imgproxy.net/#/generating_the_url_advanced?id=sharpen)
|
221
|
+
- [pixelate](https://docs.imgproxy.net/#/generating_the_url_advanced?id=pixelate) _(pro)_
|
222
|
+
- [unsharpening](https://docs.imgproxy.net/#/generating_the_url_advanced?id=unsharpening) _(pro)_
|
223
|
+
- [watermark](https://docs.imgproxy.net/#/generating_the_url_advanced?id=watermark)
|
224
|
+
- [watermark_url](https://docs.imgproxy.net/#/generating_the_url_advanced?id=watermark-url) _(pro)_
|
225
|
+
- [style](https://docs.imgproxy.net/#/generating_the_url_advanced?id=style) _(pro)_
|
226
|
+
- [jpeg_options](https://docs.imgproxy.net/#/generating_the_url_advanced?id=jpeg-options) _(pro)_
|
227
|
+
- [png_options](https://docs.imgproxy.net/#/generating_the_url_advanced?id=png-options) _(pro)_
|
228
|
+
- [gif_options](https://docs.imgproxy.net/#/generating_the_url_advanced?id=gif-options) _(pro)_
|
229
|
+
- [page](https://docs.imgproxy.net/#/generating_the_url_advanced?id=page) _(pro)_
|
230
|
+
- [video_thumbnail_second](https://docs.imgproxy.net/#/generating_the_url_advanced?id=video-thumbnail-second) _(pro)_
|
231
|
+
- [preset](https://docs.imgproxy.net/#/generating_the_url_advanced?id=preset)
|
232
|
+
- [cachebuster](https://docs.imgproxy.net/#/generating_the_url_advanced?id=cachebuster)
|
233
|
+
- [strip_metadata](https://docs.imgproxy.net/#/generating_the_url_advanced?id=strip-metadata)
|
234
|
+
- [strip_color_profile](https://docs.imgproxy.net/#/generating_the_url_advanced?id=strip-color-profile)
|
235
|
+
- [auto_rotate](https://docs.imgproxy.net/#/generating_the_url_advanced?id=auto-rotate)
|
236
|
+
- [filename](https://docs.imgproxy.net/#/generating_the_url_advanced?id=filename)
|
237
|
+
- [format](https://docs.imgproxy.net/#/generating_the_url_advanced?id=format)
|
238
|
+
|
239
|
+
_See [imgproxy URL format guide](https://docs.imgproxy.net/#/generating_the_url_advanced?id=processing-options) for more info._
|
240
|
+
|
241
|
+
### Complex processing options
|
242
|
+
|
243
|
+
Some of the processing options like `crop` or `gravity` may have multiple arguments, and you can define these arguments multiple ways:
|
244
|
+
|
245
|
+
#### Named arguments
|
246
|
+
|
247
|
+
First and the most readable way is to use a `Hash` with named arguments:
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
Imgproxy.url_for(
|
251
|
+
"http://images.example.com/images/image.jpg",
|
252
|
+
crop: {
|
253
|
+
width: 500,
|
254
|
+
height: 600,
|
255
|
+
gravity: {
|
256
|
+
type: :nowe,
|
257
|
+
x_offset: 10,
|
258
|
+
y_offset: 5
|
259
|
+
}
|
260
|
+
}
|
261
|
+
)
|
262
|
+
# => .../c:500:600:nowe:10:5/...
|
263
|
+
```
|
264
|
+
|
265
|
+
All the argguments have the same names as in [imgproxy documentation](https://docs.imgproxy.net/#/generating_the_url_advanced?id=processing-options).
|
266
|
+
|
267
|
+
You can use named arguments even if the processing option is not supported by the gem. In this case the arguments won't be reordered nor formatted, so you should provide them in the same order and right the same way they should appear in the URL:
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
Imgproxy.url_for(
|
271
|
+
"http://images.example.com/images/image.jpg",
|
272
|
+
unsupported: {
|
273
|
+
arg1: 1,
|
274
|
+
nested1: {
|
275
|
+
arg2: 2,
|
276
|
+
nested2: {
|
277
|
+
arg3: 3
|
278
|
+
}
|
279
|
+
}
|
280
|
+
}
|
281
|
+
)
|
282
|
+
# => .../unsupported:1:2:3/...
|
283
|
+
```
|
284
|
+
|
285
|
+
#### Unnamed arguments
|
286
|
+
|
287
|
+
The arguments of the complex options can be provided as an array of formatted values or even as a colon-separated string:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
Imgproxy.url_for(
|
291
|
+
"http://images.example.com/images/image.jpg",
|
292
|
+
crop: [500, 600, :nowe, 10, 5],
|
293
|
+
trim: "10:aabbcc:1:1"
|
294
|
+
)
|
295
|
+
# => .../c:500:600:nowe:10:5/t:10:aabbcc:1:1/...
|
296
|
+
```
|
297
|
+
|
298
|
+
#### Single required argument
|
299
|
+
|
300
|
+
If a complex option has a single required argument, and you don't want to use the optional ones, you can just use its value:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
Imgproxy.url_for(
|
304
|
+
"http://images.example.com/images/image.jpg",
|
305
|
+
gravity: :nowe,
|
306
|
+
trim: 10
|
307
|
+
)
|
308
|
+
# => .../g:nowe/t:10/...
|
309
|
+
```
|
310
|
+
|
311
|
+
### Base64 processing options arguments
|
312
|
+
|
313
|
+
Some of the processing options like `watermark_url` or `style` require their arguments to be base64-encoded. Good news is that imgproxy gem will encode them for you:
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
Imgproxy.url_for(
|
317
|
+
"http://images.example.com/images/image.jpg",
|
318
|
+
watermark_url: "http://example.com/watermark.jpg",
|
319
|
+
style: "color: rgba(255, 255, 255, .5)"
|
320
|
+
)
|
321
|
+
# => .../wmu:aHR0cDovL2V4YW1wbGUuY29tL3dhdGVybWFyay5qcGc/st:Y29sb3I6IHJnYmEoMjU1LCAyNTUsIDI1NSwgLjUp/...
|
322
|
+
```
|
323
|
+
|
324
|
+
### Special options:
|
325
|
+
|
326
|
+
- `base64_encode_url` — per-call redefinition of `base64_encode_urls` config.
|
327
|
+
- `escape_plain_url` — per-call redefinition of `always_escape_plain_urls` config.
|
328
|
+
- `use_short_options` — per-call redefinition of `use_short_options` config.
|
329
|
+
|
330
|
+
## Getting the image info
|
331
|
+
|
332
|
+
If you're a happy user of imgproxy Pro, you may find useful it's [Getting the image info](https://docs.imgproxy.net/#/getting_the_image_info) feature. imgproxy.rb allows you to easily generate info URLs for your images:
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
# Framework-agnositic way
|
336
|
+
Imgproxy.info_url_for("http://images.example.com/images/image.jpg")
|
337
|
+
# Using Active Storage or Shrine
|
338
|
+
user.avatar.imgproxy_info_url
|
339
|
+
|
340
|
+
# You can also use base64_encode_url or escape_plain_url options
|
341
|
+
Imgproxy.info_url_for(
|
342
|
+
"http://images.example.com/images/image.jpg",
|
343
|
+
base64_encode_url: true
|
344
|
+
)
|
345
|
+
Imgproxy.info_url_for(
|
346
|
+
"http://images.example.com/images/image.jpg",
|
347
|
+
escape_plain_url: true
|
348
|
+
)
|
349
|
+
```
|
350
|
+
|
351
|
+
## URL adapters
|
203
352
|
|
204
353
|
By default, `Imgproxy.url_for` accepts only `String` and `URI` as the source URL, but you can extend that behavior by using URL adapters.
|
205
354
|
|
@@ -226,35 +375,9 @@ Imgproxy.configure do |config|
|
|
226
375
|
end
|
227
376
|
```
|
228
377
|
|
229
|
-
**
|
230
|
-
|
231
|
-
**Note:** imgproxy.rb provides built-inadapters for Active Storage and Shrine that are automatically added by `Imgproxy.extend_active_storage!` and `Imgproxy.extend_shrine!`.
|
232
|
-
|
233
|
-
## Further configuration
|
234
|
-
|
235
|
-
### Using truncated signature
|
236
|
-
|
237
|
-
By default, the imgproxy server uses a full-length signature (32 bytes), but you can set signature length with `IMGPROXY_SIGNATURE_SIZE` environment variable. If you have configured your imgproxy server to use truncated signatures, you need to configure the gem too:
|
238
|
-
|
239
|
-
```ruby
|
240
|
-
# config/initializers/imgproxy.rb
|
241
|
-
|
242
|
-
Imgproxy.configure do |config|
|
243
|
-
config.signature_size = 5
|
244
|
-
end
|
245
|
-
```
|
246
|
-
|
247
|
-
### Using full processing options names
|
248
|
-
|
249
|
-
By default, imgproxy gem uses short processing options names (`rs` for `resize`, `g` for `gravity`, etc), but it can be configured to use full names:
|
378
|
+
**NOTE:** `Imgproxy` will use the first applicable URL adapter. If you need to add your adapter to the beginning of the list, use the `prepend` method instead of `add`.
|
250
379
|
|
251
|
-
|
252
|
-
# config/initializers/imgproxy.rb
|
253
|
-
|
254
|
-
Imgproxy.configure do |config|
|
255
|
-
config.use_short_options = false
|
256
|
-
end
|
257
|
-
```
|
380
|
+
**NOTE:** imgproxy.rb provides built-in adapters for Active Storage and Shrine that are automatically added when Active Storage or Shrine support is enabled.
|
258
381
|
|
259
382
|
## Contributing
|
260
383
|
|
data/lib/imgproxy.rb
CHANGED
@@ -19,8 +19,8 @@ module Imgproxy
|
|
19
19
|
#
|
20
20
|
# Imgproxy.configure do |config|
|
21
21
|
# config.endpoint = "http://imgproxy.example.com"
|
22
|
-
# config.
|
23
|
-
# config.
|
22
|
+
# config.key = "your_key"
|
23
|
+
# config.salt = "your_salt"
|
24
24
|
# config.use_short_options = true
|
25
25
|
# end
|
26
26
|
#
|
@@ -38,92 +38,112 @@ module Imgproxy
|
|
38
38
|
# width: 500,
|
39
39
|
# height: 400,
|
40
40
|
# resizing_type: :fill,
|
41
|
-
# sharpen: 0.5
|
41
|
+
# sharpen: 0.5,
|
42
|
+
# gravity: {
|
43
|
+
# type: :soea,
|
44
|
+
# x_offset: 10,
|
45
|
+
# y_offset: 5,
|
46
|
+
# },
|
47
|
+
# crop: {
|
48
|
+
# width: 2000,
|
49
|
+
# height: 1000,
|
50
|
+
# gravity: {
|
51
|
+
# type: :nowe,
|
52
|
+
# x_offset: 20,
|
53
|
+
# y_offset: 30,
|
54
|
+
# },
|
55
|
+
# },
|
42
56
|
# )
|
43
57
|
#
|
44
58
|
# @return [String] imgproxy URL
|
45
59
|
# @param [String,URI, Object] image Source image URL or object applicable for
|
46
60
|
# the configured URL adapters
|
47
61
|
# @param [Hash] options Processing options
|
62
|
+
# @option options [Hash|Array|String] :resize
|
63
|
+
# @option options [Hash|Array|String] :size
|
48
64
|
# @option options [String] :resizing_type
|
65
|
+
# @option options [String] :resizing_algorithm supported only by imgproxy pro
|
49
66
|
# @option options [Integer] :width
|
50
67
|
# @option options [Integer] :height
|
51
68
|
# @option options [Float] :dpr
|
52
69
|
# @option options [Boolean] :enlarge
|
53
|
-
# @option options [Boolean] :extend
|
54
|
-
# @option options [String] :
|
55
|
-
# @option options [
|
56
|
-
# @option options [Float] :extend_gravity_y
|
57
|
-
# @option options [String] :gravity
|
58
|
-
# @option options [Float] :gravity_x
|
59
|
-
# @option options [Float] :gravity_y
|
60
|
-
# @option options [Integer] :crop_width
|
61
|
-
# @option options [Integer] :crop_height
|
62
|
-
# @option options [String] :crop_gravity
|
63
|
-
# @option options [Float] :crop_gravity_x
|
64
|
-
# @option options [Float] :crop_gravity_y
|
65
|
-
# @option options [Integer] :trim_threshold
|
66
|
-
# @option options [String] :trim_color
|
70
|
+
# @option options [Hash|Array|Boolean|String] :extend
|
71
|
+
# @option options [Hash|Array|String] :gravity
|
72
|
+
# @option options [Hash|Array|String] :crop
|
67
73
|
# @option options [Array] :padding
|
68
|
-
# @option options [
|
69
|
-
# @option options [
|
74
|
+
# @option options [Hash|Array|String] :trim
|
75
|
+
# @option options [Integer] :rotate
|
70
76
|
# @option options [Integer] :quality
|
71
77
|
# @option options [Integer] :max_bytes
|
72
|
-
# @option options [Array] :background
|
78
|
+
# @option options [Array|String] :background
|
79
|
+
# @option options [Float] :background_alpha supported only by imgproxy pro
|
80
|
+
# @option options [Hash|Array|String] :adjust
|
73
81
|
# @option options [Integer] :brightness supported only by imgproxy pro
|
74
82
|
# @option options [Float] :contrast supported only by imgproxy pro
|
75
83
|
# @option options [Float] :saturation supported only by imgproxy pro
|
76
84
|
# @option options [Float] :blur
|
77
85
|
# @option options [Float] :sharpen
|
78
86
|
# @option options [Integer] :pixelate supported only by imgproxy pro
|
79
|
-
# @option options [
|
80
|
-
# @option options [String] :
|
81
|
-
# @option options [
|
82
|
-
# @option options [Integer] :watermark_y_offset
|
83
|
-
# @option options [Float] :watermark_scale
|
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
|
84
90
|
# @option options [String] :style supported only by imgproxy pro
|
91
|
+
# @option options [Hash|Array|String] :jpeg_options supported only by imgproxy pro
|
92
|
+
# @option options [Hash|Array|String] :png_options supported only by imgproxy pro
|
93
|
+
# @option options [Hash|Array|String] :gif_options supported only by imgproxy pro
|
94
|
+
# @option options [Integer] :page supported only by imgproxy pro
|
95
|
+
# @option options [Integer] :video_thumbnail_second supported only by imgproxy pro
|
85
96
|
# @option options [Array] :preset
|
86
97
|
# @option options [String] :cachebuster
|
98
|
+
# @option options [Boolean] :strip_metadata
|
99
|
+
# @option options [Boolean] :strip_color_profile
|
100
|
+
# @option options [Boolean] :auto_rotate
|
101
|
+
# @option options [String] :filename
|
87
102
|
# @option options [String] :format
|
88
103
|
# @option options [Boolean] :use_short_options
|
89
104
|
# @option options [Boolean] :base64_encode_urls
|
90
|
-
# @
|
91
|
-
#
|
105
|
+
# @option options [Boolean] :escape_plain_url
|
106
|
+
# @see https://docs.imgproxy.net/#/generating_the_url_advanced?id=processing-options
|
107
|
+
# Available imgproxy URL processing options and their arguments
|
92
108
|
def url_for(image, options = {})
|
93
109
|
Imgproxy::Builder.new(options).url_for(image)
|
94
110
|
end
|
95
111
|
|
96
|
-
#
|
97
|
-
# and adds URL adapters for ActiveStorage
|
112
|
+
# Genrates imgproxy info URL. Supported only by imgproxy pro
|
98
113
|
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
# @
|
102
|
-
# @param
|
103
|
-
|
104
|
-
|
105
|
-
|
114
|
+
# Imgproxy.info_url_for("http://images.example.com/images/image.jpg")
|
115
|
+
#
|
116
|
+
# @return [String] imgproxy info URL
|
117
|
+
# @param [String,URI, Object] image Source image URL or object applicable for
|
118
|
+
# the configured URL adapters
|
119
|
+
# @param [Hash] options Processing options
|
120
|
+
# @option options [Boolean] :base64_encode_urls
|
121
|
+
# @option options [Boolean] :escape_plain_url
|
122
|
+
def info_url_for(image, options = {})
|
123
|
+
Imgproxy::Builder.new(options).info_url_for(image)
|
124
|
+
end
|
106
125
|
|
107
|
-
|
126
|
+
# Extends +ActiveStorage::Blob+ with {Imgproxy::Extensions::ActiveStorage.imgproxy_url} method
|
127
|
+
# and adds URL adapters for ActiveStorage
|
128
|
+
def extend_active_storage!
|
129
|
+
return unless defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load)
|
108
130
|
|
109
|
-
|
110
|
-
|
111
|
-
url_adapters.add(Imgproxy::UrlAdapters::ActiveStorage.new)
|
131
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
132
|
+
::ActiveStorage::Blob.include Imgproxy::Extensions::ActiveStorage
|
133
|
+
Imgproxy.config.url_adapters.add(Imgproxy::UrlAdapters::ActiveStorage.new)
|
112
134
|
end
|
113
135
|
end
|
114
136
|
|
115
|
-
# Extends Shrine::UploadedFile with {Imgproxy::Extensions::Shrine.imgproxy_url} method
|
137
|
+
# Extends +Shrine::UploadedFile+ with {Imgproxy::Extensions::Shrine.imgproxy_url} method
|
116
138
|
# and adds URL adapters for Shrine
|
117
|
-
|
118
|
-
|
119
|
-
# @param use_s3 [Boolean] enable Amazon S3 source URLs
|
120
|
-
def extend_shrine!(host: nil, use_s3: false)
|
121
|
-
::Shrine::UploadedFile.include Imgproxy::Extensions::Shrine
|
122
|
-
|
123
|
-
url_adapters = Imgproxy.config.url_adapters
|
139
|
+
def extend_shrine!
|
140
|
+
return unless defined?(::Shrine::UploadedFile)
|
124
141
|
|
125
|
-
|
126
|
-
url_adapters.add(Imgproxy::UrlAdapters::Shrine.new
|
142
|
+
::Shrine::UploadedFile.include Imgproxy::Extensions::Shrine
|
143
|
+
Imgproxy.config.url_adapters.add(Imgproxy::UrlAdapters::Shrine.new)
|
127
144
|
end
|
128
145
|
end
|
129
146
|
end
|
147
|
+
|
148
|
+
Imgproxy.extend_active_storage!
|
149
|
+
Imgproxy.extend_shrine!
|