imgproxy 1.2.0 → 2.0.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 +235 -112
  3. data/lib/imgproxy.rb +70 -50
  4. data/lib/imgproxy/builder.rb +47 -55
  5. data/lib/imgproxy/config.rb +96 -30
  6. data/lib/imgproxy/extensions/active_storage.rb +10 -0
  7. data/lib/imgproxy/extensions/shrine.rb +10 -0
  8. data/lib/imgproxy/options.rb +83 -121
  9. data/lib/imgproxy/options_aliases.rb +43 -0
  10. data/lib/imgproxy/options_casters/adjust.rb +22 -0
  11. data/lib/imgproxy/options_casters/array.rb +12 -0
  12. data/lib/imgproxy/options_casters/base64.rb +12 -0
  13. data/lib/imgproxy/options_casters/bool.rb +12 -0
  14. data/lib/imgproxy/options_casters/crop.rb +23 -0
  15. data/lib/imgproxy/options_casters/extend.rb +26 -0
  16. data/lib/imgproxy/options_casters/float.rb +16 -0
  17. data/lib/imgproxy/options_casters/gif_options.rb +21 -0
  18. data/lib/imgproxy/options_casters/gravity.rb +23 -0
  19. data/lib/imgproxy/options_casters/group.rb +21 -0
  20. data/lib/imgproxy/options_casters/integer.rb +10 -0
  21. data/lib/imgproxy/options_casters/jpeg_options.rb +26 -0
  22. data/lib/imgproxy/options_casters/png_options.rb +23 -0
  23. data/lib/imgproxy/options_casters/resize.rb +21 -0
  24. data/lib/imgproxy/options_casters/size.rb +24 -0
  25. data/lib/imgproxy/options_casters/string.rb +10 -0
  26. data/lib/imgproxy/options_casters/trim.rb +28 -0
  27. data/lib/imgproxy/options_casters/watermark.rb +30 -0
  28. data/lib/imgproxy/trim_array.rb +11 -0
  29. data/lib/imgproxy/url_adapters.rb +0 -4
  30. data/lib/imgproxy/url_adapters/active_storage.rb +25 -0
  31. data/lib/imgproxy/url_adapters/shrine.rb +15 -5
  32. data/lib/imgproxy/version.rb +1 -1
  33. metadata +54 -9
  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
@@ -0,0 +1,43 @@
1
+ module Imgproxy
2
+ OPTIONS_ALIASES = {
3
+ resize: :rs,
4
+ size: :s,
5
+ resizing_type: :rt,
6
+ resizing_algorithm: :ra,
7
+ width: :w,
8
+ height: :h,
9
+ enlarge: :en,
10
+ extend: :ex,
11
+ gravity: :g,
12
+ crop: :c,
13
+ padding: :pd,
14
+ trim: :t,
15
+ rotate: :rot,
16
+ quality: :q,
17
+ max_bytes: :mb,
18
+ background: :bg,
19
+ background_alpha: :bga,
20
+ adjust: :a,
21
+ brightness: :br,
22
+ contrast: :co,
23
+ saturation: :sa,
24
+ blur: :bl,
25
+ sharpen: :sh,
26
+ pixelate: :pix,
27
+ unsharpening: :ush,
28
+ watermark: :wm,
29
+ watermark_url: :wmu,
30
+ style: :st,
31
+ jpeg_options: :jpego,
32
+ png_options: :pngo,
33
+ gif_options: :gifo,
34
+ page: :pg,
35
+ video_thumbnail_second: :vts,
36
+ preset: :pr,
37
+ cachebuster: :cb,
38
+ strip_metadata: :sm,
39
+ strip_color_profile: :scp,
40
+ auto_rotate: :ar,
41
+ filename: :fn,
42
+ }.freeze
43
+ end
@@ -0,0 +1,22 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/integer"
3
+ require "imgproxy/options_casters/float"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts gravity option
8
+ module Adjust
9
+ CASTER = Imgproxy::OptionsCasters::Group.new(
10
+ brightness: Imgproxy::OptionsCasters::Integer,
11
+ contrast: Imgproxy::OptionsCasters::Float,
12
+ saturation: Imgproxy::OptionsCasters::Float,
13
+ ).freeze
14
+
15
+ def self.cast(raw)
16
+ return raw unless raw.is_a?(Hash)
17
+
18
+ CASTER.cast(raw)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ module Imgproxy
2
+ module OptionsCasters
3
+ # Casts array option
4
+ module Array
5
+ def self.cast(raw)
6
+ return if raw.nil?
7
+
8
+ raw.is_a?(Array) ? raw : [raw]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require "base64"
2
+
3
+ module Imgproxy
4
+ module OptionsCasters
5
+ # Casts string option to base64
6
+ module Base64
7
+ def self.cast(raw)
8
+ ::Base64.urlsafe_encode64(raw.to_s).tr("=", "") unless raw.nil?
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Imgproxy
2
+ module OptionsCasters
3
+ # Casts boolean option
4
+ module Bool
5
+ def self.cast(raw)
6
+ return if raw.nil?
7
+
8
+ raw && raw != 0 && raw != "0" ? 1 : 0
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require "imgproxy/trim_array"
2
+ require "imgproxy/options_casters/float"
3
+ require "imgproxy/options_casters/gravity"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts crop option
8
+ module Crop
9
+ using TrimArray
10
+
11
+ def self.cast(raw)
12
+ return raw unless raw.is_a?(Hash)
13
+ return unless raw[:width] || raw[:height]
14
+
15
+ [
16
+ Imgproxy::OptionsCasters::Float.cast(raw[:width]) || 0,
17
+ Imgproxy::OptionsCasters::Float.cast(raw[:height]) || 0,
18
+ Imgproxy::OptionsCasters::Gravity.cast(raw[:gravity]),
19
+ ].trim!
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/bool"
3
+ require "imgproxy/options_casters/gravity"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts extend option
8
+ module Extend
9
+ CASTER = Imgproxy::OptionsCasters::Group.new(
10
+ extend: Imgproxy::OptionsCasters::Bool,
11
+ gravity: Imgproxy::OptionsCasters::Gravity,
12
+ ).freeze
13
+
14
+ def self.cast(raw)
15
+ # Allow extend to be just a boolean
16
+ return Imgproxy::OptionsCasters::Bool.cast(raw) if [true, false].include?(raw)
17
+
18
+ return raw unless raw.is_a?(Hash)
19
+ return if raw[:extend].nil?
20
+
21
+ values = CASTER.cast(raw)
22
+ values[0].zero? ? 0 : values
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module Imgproxy
2
+ module OptionsCasters
3
+ # Casts float option
4
+ module Float
5
+ ZERO_RE = /\.0+/.freeze
6
+
7
+ def self.cast(raw)
8
+ raw&.to_f&.yield_self do |f|
9
+ # Convert integral value to Integer so to_s won't give us trailing zero
10
+ i = f.to_i
11
+ i == f ? i : f
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/bool"
3
+
4
+ module Imgproxy
5
+ module OptionsCasters
6
+ # Casts gif_options option
7
+ module GifOptions
8
+ CASTER = Imgproxy::OptionsCasters::Group.new(
9
+ optimize_frames: Imgproxy::OptionsCasters::Bool,
10
+ optimize_transparency: Imgproxy::OptionsCasters::Bool,
11
+ ).freeze
12
+
13
+ def self.cast(raw)
14
+ return raw unless raw.is_a?(Hash)
15
+
16
+ values = CASTER.cast(raw)
17
+ values.empty? ? nil : values
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/string"
3
+ require "imgproxy/options_casters/float"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts gravity option
8
+ module Gravity
9
+ CASTER = Imgproxy::OptionsCasters::Group.new(
10
+ type: Imgproxy::OptionsCasters::String,
11
+ x_offset: Imgproxy::OptionsCasters::Float,
12
+ y_offset: Imgproxy::OptionsCasters::Float,
13
+ ).freeze
14
+
15
+ def self.cast(raw)
16
+ return raw unless raw.is_a?(Hash)
17
+ return unless raw[:type]
18
+
19
+ CASTER.cast(raw)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require "imgproxy/trim_array"
2
+
3
+ module Imgproxy
4
+ module OptionsCasters
5
+ # Casts group of options and trim nils from the end
6
+ class Group
7
+ using TrimArray
8
+
9
+ def initialize(extractors)
10
+ @extractors = extractors
11
+ end
12
+
13
+ def cast(raw)
14
+ values = @extractors.map do |key, extractor|
15
+ extractor.cast(raw[key])
16
+ end
17
+ values.trim!
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module Imgproxy
2
+ module OptionsCasters
3
+ # Casts integer option
4
+ module Integer
5
+ def self.cast(raw)
6
+ raw&.to_i
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/bool"
3
+ require "imgproxy/options_casters/integer"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts jpeg_options option
8
+ module JpegOptions
9
+ CASTER = Imgproxy::OptionsCasters::Group.new(
10
+ progressive: Imgproxy::OptionsCasters::Bool,
11
+ no_subsample: Imgproxy::OptionsCasters::Bool,
12
+ trellis_quant: Imgproxy::OptionsCasters::Bool,
13
+ overshoot_deringing: Imgproxy::OptionsCasters::Bool,
14
+ optimize_scans: Imgproxy::OptionsCasters::Bool,
15
+ quant_table: Imgproxy::OptionsCasters::Integer,
16
+ ).freeze
17
+
18
+ def self.cast(raw)
19
+ return raw unless raw.is_a?(Hash)
20
+
21
+ values = CASTER.cast(raw)
22
+ values.empty? ? nil : values
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/bool"
3
+ require "imgproxy/options_casters/integer"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts png_options option
8
+ module PngOptions
9
+ CASTER = Imgproxy::OptionsCasters::Group.new(
10
+ interlaced: Imgproxy::OptionsCasters::Bool,
11
+ quantize: Imgproxy::OptionsCasters::Bool,
12
+ quantization_colors: Imgproxy::OptionsCasters::Integer,
13
+ ).freeze
14
+
15
+ def self.cast(raw)
16
+ return raw unless raw.is_a?(Hash)
17
+
18
+ values = CASTER.cast(raw)
19
+ values.empty? ? nil : values
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require "imgproxy/trim_array"
2
+ require "imgproxy/options_casters/string"
3
+ require "imgproxy/options_casters/size"
4
+
5
+ module Imgproxy
6
+ module OptionsCasters
7
+ # Casts resize option
8
+ module Resize
9
+ using TrimArray
10
+
11
+ def self.cast(raw)
12
+ return raw unless raw.is_a?(Hash)
13
+
14
+ [
15
+ Imgproxy::OptionsCasters::String.cast(raw[:resizing_type]) || "fit",
16
+ *Imgproxy::OptionsCasters::Size.cast(raw),
17
+ ].trim!
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ require "imgproxy/trim_array"
2
+ require "imgproxy/options_casters/integer"
3
+ require "imgproxy/options_casters/bool"
4
+ require "imgproxy/options_casters/extend"
5
+
6
+ module Imgproxy
7
+ module OptionsCasters
8
+ # Casts size option
9
+ module Size
10
+ using TrimArray
11
+
12
+ def self.cast(raw)
13
+ return raw unless raw.is_a?(Hash)
14
+
15
+ [
16
+ Imgproxy::OptionsCasters::Integer.cast(raw[:width]) || 0,
17
+ Imgproxy::OptionsCasters::Integer.cast(raw[:height]) || 0,
18
+ Imgproxy::OptionsCasters::Bool.cast(raw[:enlarge]),
19
+ Imgproxy::OptionsCasters::Extend.cast(raw[:extend]),
20
+ ].trim!
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module Imgproxy
2
+ module OptionsCasters
3
+ # Casts string option
4
+ module String
5
+ def self.cast(raw)
6
+ raw&.to_s
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/float"
3
+ require "imgproxy/options_casters/string"
4
+ require "imgproxy/options_casters/bool"
5
+
6
+ module Imgproxy
7
+ module OptionsCasters
8
+ # Casts trim option
9
+ module Trim
10
+ CASTER = Imgproxy::OptionsCasters::Group.new(
11
+ threshold: Imgproxy::OptionsCasters::Float,
12
+ color: Imgproxy::OptionsCasters::String,
13
+ equal_hor: Imgproxy::OptionsCasters::Bool,
14
+ equal_ver: Imgproxy::OptionsCasters::Bool,
15
+ ).freeze
16
+
17
+ def self.cast(raw)
18
+ # Allow trim to be just a numeric
19
+ return Imgproxy::OptionsCasters::Float.cast(raw) if raw.is_a?(Numeric)
20
+
21
+ return raw unless raw.is_a?(Hash)
22
+ return unless raw[:threshold]
23
+
24
+ CASTER.cast(raw)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require "imgproxy/options_casters/group"
2
+ require "imgproxy/options_casters/float"
3
+ require "imgproxy/options_casters/string"
4
+ require "imgproxy/options_casters/integer"
5
+
6
+ module Imgproxy
7
+ module OptionsCasters
8
+ # Casts string option
9
+ module Watermark
10
+ CASTER = Imgproxy::OptionsCasters::Group.new(
11
+ opacity: Imgproxy::OptionsCasters::Float,
12
+ position: Imgproxy::OptionsCasters::String,
13
+ x_offset: Imgproxy::OptionsCasters::Integer,
14
+ y_offset: Imgproxy::OptionsCasters::Integer,
15
+ scale: Imgproxy::OptionsCasters::Float,
16
+ ).freeze
17
+
18
+ def self.cast(raw)
19
+ # Allow watermark to be just a numeric
20
+ return Imgproxy::OptionsCasters::Float.cast(raw) if raw.is_a?(Numeric)
21
+
22
+ return raw unless raw.is_a?(Hash)
23
+ return unless raw[:opacity]
24
+
25
+ values = CASTER.cast(raw)
26
+ values[0].zero? ? 0 : values
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Imgproxy
2
+ # `Array.trim!` refinement
3
+ module TrimArray
4
+ refine Array do
5
+ def trim!
6
+ delete_at(-1) while !empty? && last.nil?
7
+ self
8
+ end
9
+ end
10
+ end
11
+ end