imgproxy 1.2.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +238 -113
  3. data/lib/imgproxy/builder.rb +47 -55
  4. data/lib/imgproxy/config.rb +96 -30
  5. data/lib/imgproxy/extensions/active_storage.rb +10 -0
  6. data/lib/imgproxy/extensions/shrine.rb +10 -0
  7. data/lib/imgproxy/options.rb +86 -120
  8. data/lib/imgproxy/options_aliases.rb +45 -0
  9. data/lib/imgproxy/options_casters/adjust.rb +22 -0
  10. data/lib/imgproxy/options_casters/array.rb +12 -0
  11. data/lib/imgproxy/options_casters/base64.rb +12 -0
  12. data/lib/imgproxy/options_casters/bool.rb +12 -0
  13. data/lib/imgproxy/options_casters/crop.rb +23 -0
  14. data/lib/imgproxy/options_casters/extend.rb +26 -0
  15. data/lib/imgproxy/options_casters/float.rb +16 -0
  16. data/lib/imgproxy/options_casters/gif_options.rb +21 -0
  17. data/lib/imgproxy/options_casters/gravity.rb +23 -0
  18. data/lib/imgproxy/options_casters/group.rb +21 -0
  19. data/lib/imgproxy/options_casters/integer.rb +10 -0
  20. data/lib/imgproxy/options_casters/jpeg_options.rb +26 -0
  21. data/lib/imgproxy/options_casters/png_options.rb +23 -0
  22. data/lib/imgproxy/options_casters/resize.rb +21 -0
  23. data/lib/imgproxy/options_casters/size.rb +24 -0
  24. data/lib/imgproxy/options_casters/string.rb +10 -0
  25. data/lib/imgproxy/options_casters/trim.rb +28 -0
  26. data/lib/imgproxy/options_casters/watermark.rb +30 -0
  27. data/lib/imgproxy/trim_array.rb +11 -0
  28. data/lib/imgproxy/url_adapters/active_storage.rb +35 -0
  29. data/lib/imgproxy/url_adapters/shrine.rb +16 -6
  30. data/lib/imgproxy/url_adapters.rb +0 -4
  31. data/lib/imgproxy/version.rb +1 -1
  32. data/lib/imgproxy.rb +72 -50
  33. metadata +69 -23
  34. data/lib/imgproxy/url_adapters/active_storage_gcs.rb +0 -31
  35. data/lib/imgproxy/url_adapters/active_storage_s3.rb +0 -23
  36. 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: 138bb555a390e397e8bdc3e5f68534416bac212660f1c48aa2fc8332e5ab4828
4
- data.tar.gz: 73d3c3357b1eb2959084b827e790e2aaa2145499ec69539a069d47e02e160c43
3
+ metadata.gz: f30666a2a3a8a1097534749a93ca6d53492982f59f43e0fa91553f19957535d8
4
+ data.tar.gz: c0ac3bddc82a2e515b728107ad1bc58fedac3eb8edca76532a3f23d8b8def706
5
5
  SHA512:
6
- metadata.gz: 8891f46189c8b7a646d8fbed5202e7b66f51e63357b999158fc71c3fae19b5d3ae38a48c21f2acac7d937b6f53c512fe4f4654c528bb1a3e4a83ade50f065535
7
- data.tar.gz: 82821cd8bd891a5dc0ed51846e490b09228ac070e2d0b1dd8af544c8046fc998f628e2cd283ee8011e7d365b2a3c087241129b3fe0d8c88d13e751b260fa9b5b
6
+ metadata.gz: 3c242a4e67d4e726ebae337e9264454ad223e2fbd31e8f5655cd8a47288e6ab8ee035bebbd0d99dbe17aa9de75ed3fc098cedae1acc7501ac5ea6e3dc43feea7
7
+ data.tar.gz: ccd34093cb91dbe50d1a59139db8c94da96fe0f6dba780ae1c1538ccb91646de982055aad509adceb6a3eb6af63f9a2f9af74450ce00aaa6bb0f00439b92acae
data/README.md CHANGED
@@ -3,15 +3,15 @@
3
3
  <img align="right" width="200" height="200" title="imgproxy logo"
4
4
  src="https://cdn.rawgit.com/DarthSim/imgproxy/master/logo.svg">
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
+ [![GH Test](https://img.shields.io/github/workflow/status/imgproxy/imgproxy.rb/Test?label=Test&logo=github&style=for-the-badge)](https://github.com/imgproxy/imgproxy.rb/actions) [![GH Lint](https://img.shields.io/github/workflow/status/imgproxy/imgproxy.rb/Lint?label=Lint&logo=github&style=for-the-badge)](https://github.com/imgproxy/imgproxy.rb/actions) [![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)
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 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
+ 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
- **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.
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
- 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).
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
- Next, some basic configuration. We will use a Ruby on Rails application as an example; place the following code to `config/initializers/imgproxy.rb`:
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
- # 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
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 built-in Active Storage support. To enable it, modify your initializer at `config/initializers/imgproxy.rb`:
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 give you an URL to your user's avatar, resized to 250x250px on the fly.
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
- Imgproxy.extend_active_storage!(use_s3: true)
86
- ```
126
+ #### Amazon S3
87
127
 
88
- You can do the same if you are using Google Cloud Storage:
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
- ```ruby
91
- # config/initializers/imgproxy.rb
130
+ #### Google Cloud Storage
92
131
 
93
- Imgproxy.extend_active_storage!(use_gcs: true, gcs_bucket: "my_bucket")
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
- **Note** that you need to explicitly provide GCS bucket name since Active Storage "hides" the GCS config.
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. To enable it, modify your initializer at `config/initializers/imgproxy.rb`:
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 give you an URL to your user's avatar, resized to 250x250px on the fly.
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
- Imgproxy.extend_shrine!(host: "http://your-host.test")
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
- 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:
162
+ #### Amazon S3
131
163
 
132
- ```ruby
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,165 @@ 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
- ### Available imgproxy options
166
-
167
- * `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.
168
- * `width` — defines the width of the resulting image.
169
- * `height` — defines the height of the resulting image.
170
- * `dpr` — when set, imgproxy will multiply the image dimensions according to this factor for HiDPI (Retina) devices.
171
- * `enlarge` — when true, imgproxy will enlarge the image if it is smaller than the given size.
172
- * `extend` — when true, imgproxy will extend the image if the resizing result is smaller than the given size.
173
- * `extend_gravity`, `extend_gravity_x`, `extend_gravity_y` — define the gravity of the extend. These options accept the same values as `gravity`, `gravity_x` and `gravity_y` (see below).
174
- * `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.
175
- * `gravity_x`, `gravity_y` — specify gravity offset by X and Y axes. When `fp` gravity is used, these are floating point numbers between 0 and 1 that define the coordinates of the center of the resulting image.
176
- * `crop_width`, `crop_height` — define the size of an area of the image to be processed (crop before resize).
177
- * `crop_gravity`, `crop_gravity_x`, `crop_gravity_y` - define the gravity of the crop. These options accept the same values as `gravity`, `gravity_x` and `gravity_y` (see above).
178
- * `padding` - defines padding size in css manner (you can use array here). See [URL guide](https://docs.imgproxy.net/#/generating_the_url_advanced?id=padding) for more info.
179
- * `trim_threshold` - when set, imgproxy removes surrounding background.
180
- * `trim_color`, `trim_equal_hor`, `trim_equal_ver` - additional trim options described in the [URL guide](https://docs.imgproxy.net/#/generating_the_url_advanced?id=trim).
181
- * `quality` — defines the quality of the resulting image, percentage.
182
- * `max_bytes` — when set, imgproxy automatically degrades the quality of the image until the image is under the specified amount of bytes.
183
- * `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).
184
- * `brightness` — when set, imgproxy will adjust brightness of the resulting image. _Supported only by imgproxy pro._
185
- * `contrast` — when set, imgproxy will adjust contrast of the resulting image. _Supported only by imgproxy pro._
186
- * `saturation` — when set, imgproxy will adjust saturation of the resulting image. _Supported only by imgproxy pro._
187
- * `blur` — when set, imgproxy will apply the gaussian blur filter to the resulting image. Value is the size of a mask imgproxy will use.
188
- * `sharpen` — when set, imgproxy will apply the sharpen filter to the resulting image. Value is the size of a mask imgproxy will use.
189
- * `pixelate` — when set, imgproxy will apply the pixelate filter to the resulting image. Value is the size of a pixel. _Supported only by imgproxy pro._
190
- * `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.
191
- * `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).
192
- * `watermark_url` — when set, imgproxy will use the image from the specified URL as a watermark. _Supported only by imgproxy pro._
193
- * `style` — when set, imgproxy will prepend `<style>` node with provided content to the `<svg>` node of source SVG image. _Supported only by imgproxy pro._
194
- * `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.
195
- * `cachebuster` — defines cache buster that doesn't affect image processing but it's changing allows to bypass CDN, proxy server and browser cache.
196
- * `format` — specifies the resulting image format (`jpg`, `png`, `webp`).
197
- * `base64_encode_url` — encode the URL as a base64 string.
198
- * `use_short_options` — per-call redefinition of `use_short_options` config.
199
-
200
- **See [imgproxy URL format guide](https://github.com/imgproxy/imgproxy/blob/master/docs/generating_the_url_advanced.md) for more info.**
201
-
202
- ### URL adapters
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
+ - [return_attachment](https://docs.imgproxy.net/#/generating_the_url_advanced?id=return-attachment)
239
+ - [expires](https://docs.imgproxy.net/#/generating_the_url?id=expires)
240
+
241
+ _See [imgproxy URL format guide](https://docs.imgproxy.net/#/generating_the_url_advanced?id=processing-options) for more info._
242
+
243
+ ### Complex processing options
244
+
245
+ Some of the processing options like `crop` or `gravity` may have multiple arguments, and you can define these arguments multiple ways:
246
+
247
+ #### Named arguments
248
+
249
+ First and the most readable way is to use a `Hash` with named arguments:
250
+
251
+ ```ruby
252
+ Imgproxy.url_for(
253
+ "http://images.example.com/images/image.jpg",
254
+ crop: {
255
+ width: 500,
256
+ height: 600,
257
+ gravity: {
258
+ type: :nowe,
259
+ x_offset: 10,
260
+ y_offset: 5
261
+ }
262
+ }
263
+ )
264
+ # => .../c:500:600:nowe:10:5/...
265
+ ```
266
+
267
+ All the arguments have the same names as in [imgproxy documentation](https://docs.imgproxy.net/#/generating_the_url_advanced?id=processing-options).
268
+
269
+ 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:
270
+
271
+ ```ruby
272
+ Imgproxy.url_for(
273
+ "http://images.example.com/images/image.jpg",
274
+ unsupported: {
275
+ arg1: 1,
276
+ nested1: {
277
+ arg2: 2,
278
+ nested2: {
279
+ arg3: 3
280
+ }
281
+ }
282
+ }
283
+ )
284
+ # => .../unsupported:1:2:3/...
285
+ ```
286
+
287
+ #### Unnamed arguments
288
+
289
+ The arguments of the complex options can be provided as an array of formatted values or even as a colon-separated string:
290
+
291
+ ```ruby
292
+ Imgproxy.url_for(
293
+ "http://images.example.com/images/image.jpg",
294
+ crop: [500, 600, :nowe, 10, 5],
295
+ trim: "10:aabbcc:1:1"
296
+ )
297
+ # => .../c:500:600:nowe:10:5/t:10:aabbcc:1:1/...
298
+ ```
299
+
300
+ #### Single required argument
301
+
302
+ 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:
303
+
304
+ ```ruby
305
+ Imgproxy.url_for(
306
+ "http://images.example.com/images/image.jpg",
307
+ gravity: :nowe,
308
+ trim: 10
309
+ )
310
+ # => .../g:nowe/t:10/...
311
+ ```
312
+
313
+ ### Base64 processing options arguments
314
+
315
+ 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:
316
+
317
+ ```ruby
318
+ Imgproxy.url_for(
319
+ "http://images.example.com/images/image.jpg",
320
+ watermark_url: "http://example.com/watermark.jpg",
321
+ style: "color: rgba(255, 255, 255, .5)"
322
+ )
323
+ # => .../wmu:aHR0cDovL2V4YW1wbGUuY29tL3dhdGVybWFyay5qcGc/st:Y29sb3I6IHJnYmEoMjU1LCAyNTUsIDI1NSwgLjUp/...
324
+ ```
325
+
326
+ ### Special options:
327
+
328
+ - `base64_encode_url` — per-call redefinition of `base64_encode_urls` config.
329
+ - `escape_plain_url` — per-call redefinition of `always_escape_plain_urls` config.
330
+ - `use_short_options` — per-call redefinition of `use_short_options` config.
331
+
332
+ ## Getting the image info
333
+
334
+ 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:
335
+
336
+ ```ruby
337
+ # Framework-agnositic way
338
+ Imgproxy.info_url_for("http://images.example.com/images/image.jpg")
339
+ # Using Active Storage or Shrine
340
+ user.avatar.imgproxy_info_url
341
+
342
+ # You can also use base64_encode_url or escape_plain_url options
343
+ Imgproxy.info_url_for(
344
+ "http://images.example.com/images/image.jpg",
345
+ base64_encode_url: true
346
+ )
347
+ Imgproxy.info_url_for(
348
+ "http://images.example.com/images/image.jpg",
349
+ escape_plain_url: true
350
+ )
351
+ ```
352
+
353
+ ## URL adapters
203
354
 
204
355
  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
356
 
@@ -226,35 +377,9 @@ Imgproxy.configure do |config|
226
377
  end
227
378
  ```
228
379
 
229
- **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`.
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:
380
+ **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
381
 
251
- ```ruby
252
- # config/initializers/imgproxy.rb
253
-
254
- Imgproxy.configure do |config|
255
- config.use_short_options = false
256
- end
257
- ```
382
+ **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
383
 
259
384
  ## Contributing
260
385
 
@@ -3,6 +3,7 @@ require "base64"
3
3
  require "erb"
4
4
 
5
5
  require "imgproxy/options"
6
+ require "imgproxy/options_aliases"
6
7
 
7
8
  module Imgproxy
8
9
  # Builds imgproxy URL
@@ -17,19 +18,15 @@ module Imgproxy
17
18
  # builder.url_for("http://images.example.com/images/image1.jpg")
18
19
  # builder.url_for("http://images.example.com/images/image2.jpg")
19
20
  class Builder
20
- OMITTED_OPTIONS = %i[format].freeze
21
21
  # @param [Hash] options Processing options
22
22
  # @see Imgproxy.url_for
23
23
  def initialize(options = {})
24
24
  options = options.dup
25
25
 
26
- @base64_encode_url = options.delete(:base64_encode_url)
27
- @use_short_options = options.delete(:use_short_options)
28
-
29
- @use_short_options = config.use_short_options if @use_short_options.nil?
30
- @base64_encode_url = config.base64_encode_urls if @base64_encode_url.nil?
26
+ extract_builder_options(options)
31
27
 
32
28
  @options = Imgproxy::Options.new(options)
29
+ @format = @options.delete(:format)
33
30
  end
34
31
 
35
32
  # Genrates imgproxy URL
@@ -39,77 +36,68 @@ module Imgproxy
39
36
  # the configured URL adapters
40
37
  # @see Imgproxy.url_for
41
38
  def url_for(image)
42
- path = [*processing_options, url(image)].join("/")
39
+ path = [*processing_options, url(image, ext: @format)].join("/")
43
40
  signature = sign_path(path)
44
41
 
45
42
  File.join(Imgproxy.config.endpoint.to_s, signature, path)
46
43
  end
47
44
 
48
- private
45
+ # Genrates imgproxy info URL
46
+ #
47
+ # @return [String] imgproxy info URL
48
+ # @param [String,URI, Object] image Source image URL or object applicable for
49
+ # the configured URL adapters
50
+ # @see Imgproxy.info_url_for
51
+ def info_url_for(image)
52
+ path = url(image)
53
+ signature = sign_path(path)
49
54
 
50
- OPTIONS_ALIASES = {
51
- resize: :rs,
52
- size: :s,
53
- resizing_type: :rt,
54
- width: :w,
55
- height: :h,
56
- enlarge: :en,
57
- extend: :ex,
58
- gravity: :g,
59
- crop: :c,
60
- padding: :pd,
61
- trim: :t,
62
- quality: :q,
63
- max_bytes: :mb,
64
- background: :bg,
65
- adjust: :a,
66
- brightness: :br,
67
- contrast: :co,
68
- saturation: :sa,
69
- blur: :bl,
70
- sharpen: :sh,
71
- pixelate: :pix,
72
- watermark: :wm,
73
- watermark_url: :wmu,
74
- preset: :pr,
75
- cachebuster: :cb,
76
- }.freeze
55
+ File.join(Imgproxy.config.endpoint.to_s, "info", signature, path)
56
+ end
57
+
58
+ private
77
59
 
78
60
  NEED_ESCAPE_RE = /[@?% ]|[^\p{Ascii}]/.freeze
79
61
 
62
+ def extract_builder_options(options)
63
+ @use_short_options = not_nil_or(options.delete(:use_short_options), config.use_short_options)
64
+ @base64_encode_url = not_nil_or(options.delete(:base64_encode_url), config.base64_encode_urls)
65
+ @escape_plain_url =
66
+ not_nil_or(options.delete(:escape_plain_url), config.always_escape_plain_urls)
67
+ end
68
+
80
69
  def processing_options
81
- @processing_options ||=
82
- @options.reject { |k, _| OMITTED_OPTIONS.include?(k) }.map do |key, value|
83
- "#{option_alias(key)}:#{wrap_array(value).join(':')}"
84
- end
70
+ @processing_options ||= @options.map do |key, value|
71
+ [option_alias(key), value].join(":")
72
+ end
85
73
  end
86
74
 
87
- def plain_url_for(url)
88
- escaped_url = url.match?(NEED_ESCAPE_RE) ? ERB::Util.url_encode(url) : url
75
+ def url(image, ext: nil)
76
+ url = config.url_adapters.url_of(image)
89
77
 
90
- @options[:format] ? "plain/#{escaped_url}@#{@options[:format]}" : "plain/#{escaped_url}"
78
+ @base64_encode_url ? base64_url_for(url, ext: ext) : plain_url_for(url, ext: ext)
91
79
  end
92
80
 
93
- def base64_url_for(url)
94
- encoded_url = Base64.urlsafe_encode64(url).tr("=", "").scan(/.{1,16}/).join("/")
81
+ def plain_url_for(url, ext: nil)
82
+ escaped_url = need_escape_url?(url) ? ERB::Util.url_encode(url) : url
95
83
 
96
- @options[:format] ? "#{encoded_url}.#{@options[:format]}" : encoded_url
84
+ ext ? "plain/#{escaped_url}@#{ext}" : "plain/#{escaped_url}"
97
85
  end
98
86
 
99
- def option_alias(name)
100
- return name unless config.use_short_options
87
+ def base64_url_for(url, ext: nil)
88
+ encoded_url = Base64.urlsafe_encode64(url).tr("=", "").scan(/.{1,16}/).join("/")
101
89
 
102
- OPTIONS_ALIASES.fetch(name, name)
90
+ ext ? "#{encoded_url}.#{ext}" : encoded_url
103
91
  end
104
92
 
105
- def wrap_array(value)
106
- value.is_a?(Array) ? value : [value]
93
+ def need_escape_url?(url)
94
+ @escape_plain_url || url.match?(NEED_ESCAPE_RE)
107
95
  end
108
96
 
109
- def url(image)
110
- url = config.url_adapters.url_of(image)
97
+ def option_alias(name)
98
+ return name unless @use_short_options
111
99
 
112
- @base64_encode_url ? base64_url_for(url) : plain_url_for(url)
100
+ Imgproxy::OPTIONS_ALIASES.fetch(name, name)
113
101
  end
114
102
 
115
103
  def sign_path(path)
@@ -130,11 +118,11 @@ module Imgproxy
130
118
  end
131
119
 
132
120
  def signature_key
133
- config.key
121
+ config.raw_key
134
122
  end
135
123
 
136
124
  def signature_salt
137
- config.salt
125
+ config.raw_salt
138
126
  end
139
127
 
140
128
  def signature_size
@@ -144,5 +132,9 @@ module Imgproxy
144
132
  def config
145
133
  Imgproxy.config
146
134
  end
135
+
136
+ def not_nil_or(value, fallback)
137
+ value.nil? ? fallback : value
138
+ end
147
139
  end
148
140
  end