imgproxy 0.0.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +154 -87
- data/lib/imgproxy.rb +2 -2
- data/lib/imgproxy/builder.rb +6 -1
- data/lib/imgproxy/config.rb +2 -2
- data/lib/imgproxy/extensions/active_storage.rb +1 -1
- data/lib/imgproxy/extensions/shrine.rb +1 -1
- data/lib/imgproxy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90e13f160840d97192d6499cc36cc2bf9c5e96d58348706339f2215333c44f17
|
4
|
+
data.tar.gz: 3631ee4b306561b4f7ab00d8930e80a510013fd37dba059b825e21aa115d6977
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b519665418fabeda9bbc356454952e4490163cfeea490fa620aae7a475df95d8c58c1fe051e567b650733d9bd388b26c3f5344c638a238a22c4bb6fd2b889018
|
7
|
+
data.tar.gz: 37c777751a3f6d0384cafd4f08e84b02c4f26221b61550605e95c9c6ffec05e5be5cbe2c8030a84efca6c39ab5c41c8997e3a87bb3b58b268cf0c907a5215a49
|
data/README.md
CHANGED
@@ -1,163 +1,199 @@
|
|
1
1
|
# imgproxy.rb
|
2
2
|
|
3
|
-
|
3
|
+
<img align="right" width="200" height="200" title="imgproxy logo"
|
4
|
+
src="https://cdn.rawgit.com/DarthSim/imgproxy/master/logo.svg">
|
4
5
|
|
5
|
-
|
6
|
+
[![CircleCI branch](https://img.shields.io/circleci/project/github/imgproxy/imgproxy.rb/master.svg?style=for-the-badge)](https://circleci.com/gh/imgproxy/imgproxy.rb) [![Gem](https://img.shields.io/gem/v/imgproxy.svg?style=for-the-badge)](https://rubygems.org/gems/imgproxy) [![rubydoc.org](https://img.shields.io/badge/rubydoc-reference-blue.svg?style=for-the-badge)](https://www.rubydoc.info/gems/imgproxy)
|
6
7
|
|
7
|
-
[imgproxy](https://github.com/
|
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.
|
8
9
|
|
9
|
-
|
10
|
+
imgproxy can be used to provide a fast and secure way to replace all the image resizing code of your web application (like calling ImageMagick or GraphicsMagick, or using libraries), while also being able to resize everything on the fly, fast and easy. imgproxy is also indispensable when handling lots of image resizing, especially when images come from a remote source.
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
**imgproxy.rb** is a Ruby Gem for imgproxy that is framework-agnostic, but includes proper support for Ruby on Rails' most popular image attachment options.
|
13
|
+
|
14
|
+
imgproxy.rb provides easy configuration and URL generation as well as plug&play support for [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) and [Shrine](https://github.com/shrinerb/shrine).
|
15
|
+
|
16
|
+
<a href="https://evilmartians.com/?utm_source=imgproxy.rb">
|
17
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
18
|
+
</a>
|
19
|
+
|
20
|
+
## Installation
|
14
21
|
|
15
|
-
|
22
|
+
Add this to your `Gemfile`:
|
16
23
|
|
17
24
|
```ruby
|
18
25
|
gem "imgproxy"
|
19
26
|
```
|
20
27
|
|
28
|
+
or install system-wide:
|
29
|
+
|
30
|
+
```
|
31
|
+
gem install imgproxy
|
32
|
+
```
|
33
|
+
|
21
34
|
## Configuration
|
22
35
|
|
36
|
+
Next, some basic configuration. We will use a Ruby on Rails application as an example; place the following code to `config/initializers/imgproxy.rb`:
|
37
|
+
|
23
38
|
```ruby
|
24
39
|
# config/initializers/imgproxy.rb
|
25
40
|
|
26
41
|
Imgproxy.configure do |config|
|
27
42
|
# imgproxy endpoint
|
43
|
+
#
|
44
|
+
# Full URL to where your imgproxy lives.
|
28
45
|
config.endpoint = "http://imgproxy.example.com"
|
29
|
-
|
46
|
+
|
47
|
+
# Next, you have to provide your signature key and salt.
|
48
|
+
# If unsure, check out https://github.com/imgproxy/imgproxy/blob/master/docs/configuration.md first.
|
49
|
+
|
50
|
+
# Hex-encoded signature key
|
30
51
|
config.hex_key = "your_key"
|
31
|
-
#
|
52
|
+
# Hex-encoded signature salt
|
32
53
|
config.hex_salt = "your_salt"
|
33
|
-
# signature size. Defaults to 32
|
34
|
-
config.signature_size = 5
|
35
|
-
# use short processing option names (`rs` for `resize`, `g` for `gravity`, etc).
|
36
|
-
# Defaults to true
|
37
|
-
config.use_short_options = false
|
38
54
|
end
|
39
55
|
```
|
40
56
|
|
41
57
|
## Usage
|
42
58
|
|
43
|
-
|
44
|
-
Imgproxy.url_for(
|
45
|
-
"http://images.example.com/images/image.jpg",
|
46
|
-
width: 500,
|
47
|
-
height: 400,
|
48
|
-
resizing_type: :fill,
|
49
|
-
sharpen: 0.5
|
50
|
-
)
|
51
|
-
# => http://imgproxy.example.com/2tjGMpWqjO/rs:fill:500:400/sh:0.5/plain/http://images.example.com/images/image.jpg
|
52
|
-
```
|
59
|
+
### Using with Active Storage
|
53
60
|
|
54
|
-
|
61
|
+
imgproxy.rb comes with built-in Active Storage support. To enable it, modify your initializer at `config/initializers/imgproxy.rb`:
|
55
62
|
|
56
63
|
```ruby
|
57
|
-
|
58
|
-
width: 500,
|
59
|
-
height: 400,
|
60
|
-
resizing_type: :fill,
|
61
|
-
sharpen: 0.5
|
62
|
-
)
|
64
|
+
# config/initializers/imgproxy.rb
|
63
65
|
|
64
|
-
|
65
|
-
builder.url_for("http://images.example.com/images/image2.jpg")
|
66
|
+
Imgproxy.extend_active_storage!
|
66
67
|
```
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
* `resizing_type`
|
71
|
-
* `width`
|
72
|
-
* `height`
|
73
|
-
* `dpr`
|
74
|
-
* `enlarge`
|
75
|
-
* `extend`
|
76
|
-
* `gravity`
|
77
|
-
* `gravity_x`
|
78
|
-
* `gravity_y`
|
79
|
-
* `quality`
|
80
|
-
* `background`
|
81
|
-
* `blur`
|
82
|
-
* `sharpen`
|
83
|
-
* `watermark_opacity`
|
84
|
-
* `watermark_position`
|
85
|
-
* `watermark_x_offset`
|
86
|
-
* `watermark_y_offset`
|
87
|
-
* `watermark_scale`
|
88
|
-
* `preset`
|
89
|
-
* `cachebuster`
|
90
|
-
* `format`
|
91
|
-
* `use_short_options`
|
92
|
-
|
93
|
-
_See [imgproxy URL format guide](https://github.com/DarthSim/imgproxy/blob/master/docs/generating_the_url_advanced.md) for more info_
|
94
|
-
|
95
|
-
### Using with ActiveStorage
|
96
|
-
|
97
|
-
If you use [ActiveStorage](https://guides.rubyonrails.org/active_storage_overview.html), you can configure imgproxy gem to work with ActiveStorage attachments:
|
69
|
+
Now, to add imgproxy processing to your image attachments, just use the `imgproxy_url` method:
|
98
70
|
|
99
71
|
```ruby
|
100
|
-
Imgproxy.extend_active_storage
|
101
|
-
|
102
|
-
# Now you can use ActiveStorage attachment as a source URL
|
103
|
-
Imgproxy.url_for(user.avatar, width: 250, height: 250)
|
104
|
-
# or you can use #imgproxy_url method of an attachment
|
105
72
|
user.avatar.imgproxy_url(width: 250, height: 250)
|
106
73
|
```
|
107
74
|
|
108
|
-
|
75
|
+
will give you an URL to your user's avatar, resized to 250x250px on the fly.
|
76
|
+
|
77
|
+
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:
|
109
78
|
|
110
79
|
```ruby
|
111
|
-
|
80
|
+
# config/initializers/imgproxy.rb
|
81
|
+
|
82
|
+
Imgproxy.extend_active_storage!(use_s3: true)
|
112
83
|
```
|
113
84
|
|
114
|
-
You can do the same
|
85
|
+
You can do the same if you are using Google Cloud Storage:
|
115
86
|
|
116
87
|
```ruby
|
117
|
-
#
|
118
|
-
|
88
|
+
# config/initializers/imgproxy.rb
|
89
|
+
|
90
|
+
Imgproxy.extend_active_storage!(use_gcs: true, gcs_bucket: "my_bucket")
|
119
91
|
```
|
120
92
|
|
93
|
+
**Note** that you need to explicitly provide GCS bucket name since Active Storage "hides" the GCS config.
|
94
|
+
|
121
95
|
### Using with Shrine
|
122
96
|
|
123
|
-
|
97
|
+
You can also use imgproxy.rb's built-in [Shrine](https://github.com/shrinerb/shrine) support. To enable it, modify your initializer at `config/initializers/imgproxy.rb`:
|
124
98
|
|
125
99
|
```ruby
|
126
|
-
|
100
|
+
# config/initializers/imgproxy.rb
|
127
101
|
|
128
|
-
|
129
|
-
|
130
|
-
|
102
|
+
Imgproxy.extend_shrine!
|
103
|
+
```
|
104
|
+
|
105
|
+
Now you can use `imgproxy_url` method of `Shrine::UploadedFile`:
|
106
|
+
|
107
|
+
```ruby
|
131
108
|
user.avatar.imgproxy_url(width: 250, height: 250)
|
132
109
|
```
|
133
110
|
|
134
|
-
|
111
|
+
will give you an URL to your user's avatar, resized to 250x250px on the fly.
|
112
|
+
|
113
|
+
**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, initialize `Shrine::Storage::FileSystem` with the `host` option:
|
135
114
|
|
136
115
|
```ruby
|
116
|
+
# Your Shrine initializer
|
117
|
+
|
137
118
|
Shrine.storages = {
|
138
119
|
store: Shrine::Storage::FileSystem.new("public", host: "http://your-host.test")
|
139
120
|
}
|
140
121
|
```
|
141
122
|
|
142
|
-
|
123
|
+
Alternatively, you can launch your imgproxy server with the `IMGPROXY_BASE_URL` setting:
|
143
124
|
|
144
125
|
```
|
145
126
|
IMGPROXY_BASE_URL="http://your-host.test" imgproxy
|
146
127
|
```
|
147
128
|
|
148
|
-
If you configured both your imgproxy server and Shrine
|
129
|
+
If you have configured both your imgproxy server and Shrine to work with Amazon S3, you may want to use the short `s3://...` source URL scheme instead of the long one generated by Rails:
|
149
130
|
|
150
131
|
```ruby
|
151
|
-
Imgproxy.extend_shrine(use_s3: true)
|
132
|
+
Imgproxy.extend_shrine!(use_s3: true)
|
152
133
|
```
|
153
134
|
|
135
|
+
### Usage in a framework-agnostic way
|
136
|
+
|
137
|
+
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:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
Imgproxy.url_for(
|
141
|
+
"http://images.example.com/images/image.jpg",
|
142
|
+
width: 500,
|
143
|
+
height: 400,
|
144
|
+
resizing_type: :fill,
|
145
|
+
sharpen: 0.5
|
146
|
+
)
|
147
|
+
# => http://imgproxy.example.com/2tjGMpWqjO/rs:fill:500:400/sh:0.5/plain/http://images.example.com/images/image.jpg
|
148
|
+
```
|
149
|
+
|
150
|
+
You can reuse processing options by using `Imgproxy::Builder`:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
builder = Imgproxy::Builder.new(
|
154
|
+
width: 500,
|
155
|
+
height: 400,
|
156
|
+
resizing_type: :fill,
|
157
|
+
sharpen: 0.5
|
158
|
+
)
|
159
|
+
|
160
|
+
builder.url_for("http://images.example.com/images/image1.jpg")
|
161
|
+
builder.url_for("http://images.example.com/images/image2.jpg")
|
162
|
+
```
|
163
|
+
|
164
|
+
### Available imgproxy options
|
165
|
+
|
166
|
+
* `resizing_type` — defines how imgproxy will resize the image. See [URL format guide](https://github.com/imgproxy/imgproxy/blob/master/docs/generating_the_url_advanced.md#resizing-type) for available values.
|
167
|
+
* `width` — defines the width of the resulting image.
|
168
|
+
* `height` — defines the height of the resulting image.
|
169
|
+
* `dpr` — when set, imgproxy will multiply the image dimensions according to this factor for HiDPI (Retina) devices.
|
170
|
+
* `enlarge` — when true, imgproxy will enlarge the image if it is smaller than the given size.
|
171
|
+
* `extend` — when true, imgproxy will extend the image if the resizing result is smaller than the given size.
|
172
|
+
* `gravity` — defines gravity that will be used when imgproxy needs to cut some parts of the image. See [URL format guide](https://github.com/imgproxy/imgproxy/blob/master/docs/generating_the_url_advanced.md#gravity) for available values.
|
173
|
+
* `gravity_x`, `gravity_y` — floating point numbers between 0 and 1 that define the coordinates of the center of the resulting image when `fp` gravity is used.
|
174
|
+
* `quality` — defines the quality of the resulting image, percentage.
|
175
|
+
* `background` — when set, imgproxy will fill the resulting image background with the specified color. Can be a hex-color string or an array of red, green and blue values (0-255).
|
176
|
+
* `blur` — when set, imgproxy will apply the gaussian blur filter to the resulting image. Value is the size of a mask imgproxy will use.
|
177
|
+
* `sharpen` — when set, imgproxy will apply the sharpen filter to the resulting image. Value is the size of a mask imgproxy will use.
|
178
|
+
* `watermark_opacity` — when set, imgproxy will put a watermark on the resulting image. See [watermars guide](https://github.comimgproxym/imgproxy/blob/master/docs/watermark.md) for more info.
|
179
|
+
* `watermark_position`, `watermark_x_offset`, `watermark_y_offset`, `watermark_scale` — additional watermark options described in the [watermars guide](https://github.com/imgproxy/imgproxy/blob/master/docs/watermark.md).
|
180
|
+
* `preset` — array of names of presets that will be used by imgproxy. See [presets guide](https://github.com/imgproxy/imgproxy/blob/master/docs/presets.md) for more info.
|
181
|
+
* `cachebuster` — defines cache buster that doesn't affect image processing but it's changing allows to bypass CDN, proxy server and browser cache.
|
182
|
+
* `format` — specifies the resulting image format (`jpg`, `png`, `webp`).
|
183
|
+
* `use_short_options` — per-call redefinition of `use_short_options` config.
|
184
|
+
|
185
|
+
**See [imgproxy URL format guide](https://github.com/imgproxy/imgproxy/blob/master/docs/generating_the_url_advanced.md) for more info.**
|
186
|
+
|
154
187
|
### URL adapters
|
155
188
|
|
156
|
-
By default, `Imgproxy.url_for` accepts only `String`
|
189
|
+
By default, `Imgproxy.url_for` accepts only `String` and `URI` as the source URL, but you can extend that behavior by using URL adapters.
|
190
|
+
|
191
|
+
URL adapter is a simple class that implements `applicable?` and `url` methods. See the example below:
|
157
192
|
|
158
193
|
```ruby
|
159
194
|
class MyItemAdapter
|
160
|
-
# `applicable?` checks if the adapter can extract
|
195
|
+
# `applicable?` checks if the adapter can extract
|
196
|
+
# source URL from the provided object
|
161
197
|
def applicable?(item)
|
162
198
|
item.is_a? MyItem
|
163
199
|
end
|
@@ -168,18 +204,49 @@ class MyItemAdapter
|
|
168
204
|
end
|
169
205
|
end
|
170
206
|
|
207
|
+
# ...
|
208
|
+
|
171
209
|
Imgproxy.configure do |config|
|
172
210
|
config.url_adapters.add MyItemAdapter.new
|
173
211
|
end
|
174
212
|
```
|
175
213
|
|
176
|
-
**Note:** `Imgproxy` will use the first applicable URL adapter. If you need to add your adapter to the beginning of the list, use `prepend` method instead of `add`.
|
214
|
+
**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`.
|
215
|
+
|
216
|
+
**Note:** imgproxy.rb provides built-inadapters for Active Storage and Shrine that are automatically added by `Imgproxy.extend_active_storage!` and `Imgproxy.extend_shrine!`.
|
217
|
+
|
218
|
+
## Further configuration
|
219
|
+
|
220
|
+
### Using truncated signature
|
221
|
+
|
222
|
+
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:
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
# config/initializers/imgproxy.rb
|
226
|
+
|
227
|
+
Imgproxy.configure do |config|
|
228
|
+
config.signature_size = 5
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
### Using full processing options names
|
177
233
|
|
178
|
-
imgproxy gem
|
234
|
+
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:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
# config/initializers/imgproxy.rb
|
238
|
+
|
239
|
+
Imgproxy.configure do |config|
|
240
|
+
config.use_short_options = false
|
241
|
+
end
|
242
|
+
```
|
179
243
|
|
180
244
|
## Contributing
|
181
245
|
|
182
246
|
Bug reports and pull requests are welcome on GitHub at https://github.com/imgproxy/imgproxy.rb.
|
183
247
|
|
248
|
+
If you are having any problems with image processing of imgproxy itself, be sure to visit https://github.com/imgproxy/imgproxy first and check out the docs at https://github.com/imgproxy/imgproxy/blob/master/docs/.
|
249
|
+
|
184
250
|
## License
|
251
|
+
|
185
252
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/imgproxy.rb
CHANGED
@@ -80,7 +80,7 @@ module Imgproxy
|
|
80
80
|
# @param use_s3 [Boolean] enable Amazon S3 source URLs
|
81
81
|
# @param use_gcs [Boolean] enable Google Cloud Storage source URLs
|
82
82
|
# @param gcs_bucket [String] Google Cloud Storage bucket name
|
83
|
-
def extend_active_storage(use_s3: false, use_gcs: false, gcs_bucket: nil)
|
83
|
+
def extend_active_storage!(use_s3: false, use_gcs: false, gcs_bucket: nil)
|
84
84
|
ActiveSupport.on_load(:active_storage_blob) do
|
85
85
|
::ActiveStorage::Blob.include Imgproxy::Extensions::ActiveStorage
|
86
86
|
|
@@ -97,7 +97,7 @@ module Imgproxy
|
|
97
97
|
#
|
98
98
|
# @return [void]
|
99
99
|
# @param use_s3 [Boolean] enable Amazon S3 source URLs
|
100
|
-
def extend_shrine(use_s3: false)
|
100
|
+
def extend_shrine!(use_s3: false)
|
101
101
|
::Shrine::UploadedFile.include Imgproxy::Extensions::Shrine
|
102
102
|
|
103
103
|
url_adapters = Imgproxy.config.url_adapters
|
data/lib/imgproxy/builder.rb
CHANGED
@@ -85,7 +85,7 @@ module Imgproxy
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def sign_path(path)
|
88
|
-
return "unsafe"
|
88
|
+
return "unsafe" unless ready_to_sign?
|
89
89
|
|
90
90
|
digest = OpenSSL::HMAC.digest(
|
91
91
|
OpenSSL::Digest.new("sha256"),
|
@@ -96,6 +96,11 @@ module Imgproxy
|
|
96
96
|
Base64.urlsafe_encode64(digest).tr("=", "")
|
97
97
|
end
|
98
98
|
|
99
|
+
def ready_to_sign?
|
100
|
+
!(signature_key.nil? || signature_salt.nil? ||
|
101
|
+
signature_key.empty? || signature_salt.empty?)
|
102
|
+
end
|
103
|
+
|
99
104
|
def signature_key
|
100
105
|
config.key
|
101
106
|
end
|
data/lib/imgproxy/config.rb
CHANGED
@@ -26,14 +26,14 @@ module Imgproxy
|
|
26
26
|
#
|
27
27
|
# @param value [String] hex-encoded signature key
|
28
28
|
def hex_key=(value)
|
29
|
-
self.key = [value].pack("H*")
|
29
|
+
self.key = value.nil? ? nil : [value].pack("H*")
|
30
30
|
end
|
31
31
|
|
32
32
|
# Decodes hex-encoded salt and sets it to {#salt}
|
33
33
|
#
|
34
34
|
# @param value [String] hex-encoded signature salt
|
35
35
|
def hex_salt=(value)
|
36
|
-
self.salt = [value].pack("H*")
|
36
|
+
self.salt = value.nil? ? nil : [value].pack("H*")
|
37
37
|
end
|
38
38
|
|
39
39
|
def endpoint=(value)
|
data/lib/imgproxy/version.rb
CHANGED