imgproxy-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b65c92558467f59a8a6c5d94459875aa1e7b9bcdbb6b97a978af6f91e917e6c
4
+ data.tar.gz: 56271c6e0cfd75ddcb4f4307e90702eeb5705f886bcee122523594922793350f
5
+ SHA512:
6
+ metadata.gz: eb8011064eb7798f58d98db9b0dcb2c3d598c6f00c531c5c452836a7ec8ba8a64c8763db4397793058adda12f914c9bd46fd68faf44c113feb9088f786cc3d2d
7
+ data.tar.gz: eee0ab8edd73aaa2e1f9c9fd064bc6b9b30ea3b015abea239ccfbc1bd45674b85d4b950a2541d133da475846b3c274b89052a83dff592e5b383a40d744b86f69
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Change log
2
+
3
+ ## master
4
+
5
+ - Initial implementation. ([@Bakaface][])
6
+
7
+ [@palkan]: https://github.com/palkan
8
+ [@Bakaface]: https://github.com/Bakaface
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2023 Evil Martians
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ [![Gem Version](https://badge.fury.io/rb/imgproxy-rails.svg)](https://rubygems.org/gems/imgproxy-rails)
2
+ [![Build](https://github.com/palkan/imgproxy-rails/workflows/Build/badge.svg)](https://github.com/palkan/imgproxy-rails/actions)
3
+ [![JRuby Build](https://github.com/palkan/imgproxy-rails/workflows/Build%20JRuby/badge.svg)](https://github.com/palkan/imgproxy-rails/actions)
4
+
5
+ # imgproxy-rails
6
+
7
+ Integration of [imgproxy.rb](https://github.com/imgproxy/imgproxy.rb) with [ActiveStorage::Variant API](https://edgeapi.rubyonrails.org/classes/ActiveStorage/Variant.htmlasses/ActiveStorage/Variant.html).
8
+
9
+ ## Installation
10
+
11
+ Add to your project:
12
+
13
+ ```ruby
14
+ # Gemfile
15
+ gem "imgproxy-rails"
16
+ ```
17
+
18
+ ### Supported Ruby versions
19
+
20
+ - Ruby (MRI) >= 2.7.0
21
+ - JRuby >= 9.3.0
22
+
23
+ ### Supported Rails versions
24
+
25
+ - Rails >= 6.0.0
26
+
27
+ ## Usage
28
+
29
+ Given the following configuration:
30
+
31
+ ```ruby
32
+ # development.rb
33
+ config.active_storage.resolve_model_to_route = :rails_storage_proxy
34
+
35
+ # production.rb
36
+ config.active_storage.resolve_model_to_route = :imgproxy_active_storage
37
+ ```
38
+
39
+ The following HTML snippet will generate different URLs in dev and prod:
40
+
41
+ ```erb
42
+ # show.erb.html
43
+ <%= image_tag Current.user.avatar.variant(resize: "100x100") %>
44
+ ```
45
+
46
+ In dev, it will generate a URL like this:
47
+
48
+ ```html
49
+ <img src="http://localhost:3000/rails/active_storage/blobs/proxy/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBWHc9IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--0c35e9a616c29da2dfa10a385bae7172526e7961/me.png">
50
+ ```
51
+
52
+ In prod, it will generate a URL like this:
53
+
54
+ ```html
55
+ <img src="https://imgproxy.example.com/8uVB2dYrZVOdG-1tekFjNJZ7s7VHDViXJbu9TcQavQ8/fn:me.png/aHR0cDovL2xvY2Fs/aG9zdDozMDAwL3Jh/aWxzL2FjdGl2ZV9z/dG9yYWdlL2Jsb2Jz/L3Byb3h5L2V5SmZj/bUZwYkhNaU9uc2li/V1Z6YzJGblpTSTZJ/a0pCYUhCQldIYzlJ/aXdpWlhod0lqcHVk/V3hzTENKd2RYSWlP/aUppYkc5aVgybGtJ/bjE5LS0wYzM1ZTlh/NjE2YzI5ZGEyZGZh/MTBhMzg1YmFlNzE3/MjUyNmU3OTYxL21l/LnBuZw">
56
+ ```
57
+
58
+ You can also specify imgproxy-specific parameters in `imgproxy_options` attribute. Imgproxy-specific params will take precedence over ones from original API:
59
+
60
+ ```ruby
61
+ # height=50 and width=50 will be applied
62
+ Current.user.avatar.variant(resize: "100x100", imgproxy_options: {height: 50, width: 50})
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/imgproxy-rails](https://github.com/palkan/imgproxy-rails).
68
+
69
+ ## Credits
70
+
71
+ This gem is generated via [new-gem-generator](https://github.com/palkan/new-gem-generator).
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImgproxyRails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImgproxyRails
4
+ module Helpers
5
+ def self.image_variation?(model)
6
+ model.respond_to?(:variation) &&
7
+ model.try(:blob)&.content_type&.split("/")&.first == "image"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImgproxyRails
4
+ class Transformer
5
+ MAP = {
6
+ resize: proc do |p|
7
+ width, height = p.split("x")
8
+ {width: width, height: height}
9
+ end,
10
+ resize_to_limit: proc { |p| {width: p[0], height: p[1]} },
11
+ resize_to_fit: proc { |p| {width: p[0], height: p[1]} },
12
+ resize_to_fill: proc { |p| {width: p[0], height: p[1], resizing_type: :fill} },
13
+ resize_and_pad: proc { |p, m| resize_and_pad(p, m) },
14
+ convert: proc { |p| {format: p} },
15
+ trim: proc { {trim: 0} },
16
+ modulate: proc { |p| modulate(p) }
17
+ }.freeze
18
+
19
+ PASSTHROUGH_OPTIONS = Set.new([
20
+ "rotate",
21
+ "sharpen",
22
+ "blur",
23
+ "quality"
24
+ ]).freeze
25
+
26
+ class << self
27
+ def call(transformations, meta)
28
+ passed_options = transformations.delete(:imgproxy_options) || {}
29
+ mapped_options = transformations.each_with_object({}) do |(t_key, t_value), memo|
30
+ if PASSTHROUGH_OPTIONS.include?(t_key.to_s)
31
+ memo[t_key] = t_value
32
+ next
33
+ end
34
+ memo.merge!(MAP[t_key].call(t_value, meta)) if MAP.key?(t_key)
35
+ end
36
+ mapped_options.merge(passed_options)
37
+ end
38
+
39
+ private
40
+
41
+ def convert_color(color)
42
+ return unless color && color =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
43
+
44
+ color.sub(/^#/, "")
45
+ end
46
+
47
+ def resize_and_pad(p, m)
48
+ target_width, target_height, options = p
49
+ options ||= {}
50
+
51
+ result = {width: target_width, height: target_height}
52
+ return result unless m["width"] && m["height"]
53
+
54
+ aspect_ratio = m["width"].to_f / m["height"]
55
+ if aspect_ratio > 1
56
+ # add vertical padding
57
+ final_height = target_width.to_f / aspect_ratio
58
+ padding_length = ((target_height - final_height) / 2).round
59
+ result[:padding] = [padding_length, 0]
60
+
61
+ # setting min-width for correct upscaling
62
+ result[:mw] = target_width
63
+ elsif aspect_ratio < 1
64
+ # add horizontal padding
65
+ final_width = target_height.to_f * aspect_ratio
66
+ padding_length = ((target_width - final_width) / 2).round
67
+ result[:padding] = [0, padding_length]
68
+
69
+ # setting min-height for correct upscaling
70
+ result[:mh] = target_height
71
+ end
72
+
73
+ if (background = convert_color(options[:background]))
74
+ result[:background] = background
75
+ end
76
+
77
+ result
78
+ end
79
+
80
+ def modulate(p)
81
+ brightness, saturation, _ = p.split(",").map(&:to_i)
82
+
83
+ result = {}
84
+ result[:brightness] = brightness if brightness
85
+ result[:saturation] = saturation if saturation
86
+
87
+ result
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImgproxyRails
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "imgproxy"
4
+
5
+ require "imgproxy-rails/engine"
6
+ require "imgproxy-rails/transformer"
7
+ require "imgproxy-rails/helpers"
8
+ require "imgproxy-rails/version"
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imgproxy-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Dementyev
8
+ - Albert Pazderin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: imgproxy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activestorage
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '6.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '6.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.15'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '1.15'
56
+ - !ruby/object:Gem::Dependency
57
+ name: combustion
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '1.1'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '1.1'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '13.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '13.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '3.9'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '3.9'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec-rails
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '4.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '4.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rails
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: 7.0.3
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: 7.0.3
126
+ - !ruby/object:Gem::Dependency
127
+ name: image_processing
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '1.2'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '1.2'
140
+ description: A gem that integrates imgproxy.rb with ActiveStorage::Variant API
141
+ email:
142
+ - dementiev.vm@gmail.com
143
+ - afaceisnomore@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - CHANGELOG.md
149
+ - LICENSE.txt
150
+ - README.md
151
+ - lib/imgproxy-rails.rb
152
+ - lib/imgproxy-rails/engine.rb
153
+ - lib/imgproxy-rails/helpers.rb
154
+ - lib/imgproxy-rails/transformer.rb
155
+ - lib/imgproxy-rails/version.rb
156
+ homepage: http://github.com/evilmartians/imgproxy-rails
157
+ licenses:
158
+ - MIT
159
+ metadata:
160
+ bug_tracker_uri: http://github.com/evilmartians/imgproxy-rails/issues
161
+ changelog_uri: https://github.com/evilmartians/imgproxy-rails/blob/master/CHANGELOG.md
162
+ documentation_uri: http://github.com/evilmartians/imgproxy-rails
163
+ homepage_uri: http://github.com/evilmartians/imgproxy-rails
164
+ source_code_uri: http://github.com/evilmartians/imgproxy-rails
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '2.7'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.4.8
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: integration of imgproxy.rb with ActiveStorage::Variant API
184
+ test_files: []