imgproxy 2.0.1 → 3.0.0

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +8 -0
  3. data/CHANGELOG.md +74 -0
  4. data/README.md +176 -133
  5. data/UPGRADE.md +136 -0
  6. data/docs/info_options.md +223 -0
  7. data/docs/processing_options.md +724 -0
  8. data/docs/yard/fix_pictures.rb +25 -0
  9. data/docs/yard/github_alerts.rb +16 -0
  10. data/docs/yard/relative_markdown_links.rb +31 -0
  11. data/lib/imgproxy/config.rb +96 -41
  12. data/lib/imgproxy/extensions/active_storage.rb +6 -4
  13. data/lib/imgproxy/extensions/shrine.rb +6 -4
  14. data/lib/imgproxy/option_aliases/info.rb +33 -0
  15. data/lib/imgproxy/option_aliases/processing.rb +74 -0
  16. data/lib/imgproxy/options_builders/base.rb +54 -0
  17. data/lib/imgproxy/options_builders/info.rb +52 -0
  18. data/lib/imgproxy/options_builders/processing.rb +150 -0
  19. data/lib/imgproxy/options_casters/adjust.rb +3 -1
  20. data/lib/imgproxy/options_casters/alpha.rb +27 -0
  21. data/lib/imgproxy/options_casters/array.rb +3 -1
  22. data/lib/imgproxy/options_casters/autoquality.rb +28 -0
  23. data/lib/imgproxy/options_casters/average.rb +27 -0
  24. data/lib/imgproxy/options_casters/background.rb +27 -0
  25. data/lib/imgproxy/options_casters/base64.rb +2 -0
  26. data/lib/imgproxy/options_casters/blur_detections.rb +28 -0
  27. data/lib/imgproxy/options_casters/blurhash.rb +21 -0
  28. data/lib/imgproxy/options_casters/bool.rb +3 -1
  29. data/lib/imgproxy/options_casters/crop.rb +3 -1
  30. data/lib/imgproxy/options_casters/dominant_colors.rb +27 -0
  31. data/lib/imgproxy/options_casters/draw_detections.rb +28 -0
  32. data/lib/imgproxy/options_casters/extend.rb +3 -1
  33. data/lib/imgproxy/options_casters/filename.rb +31 -0
  34. data/lib/imgproxy/options_casters/float.rb +4 -2
  35. data/lib/imgproxy/options_casters/format_quality.rb +19 -0
  36. data/lib/imgproxy/options_casters/gradient.rb +31 -0
  37. data/lib/imgproxy/options_casters/gravity.rb +20 -6
  38. data/lib/imgproxy/options_casters/group.rb +2 -0
  39. data/lib/imgproxy/options_casters/hashsum.rb +24 -0
  40. data/lib/imgproxy/options_casters/integer.rb +2 -0
  41. data/lib/imgproxy/options_casters/jpeg_options.rb +3 -1
  42. data/lib/imgproxy/options_casters/padding.rb +41 -0
  43. data/lib/imgproxy/options_casters/png_options.rb +3 -1
  44. data/lib/imgproxy/options_casters/resize.rb +3 -1
  45. data/lib/imgproxy/options_casters/size.rb +3 -1
  46. data/lib/imgproxy/options_casters/string.rb +2 -0
  47. data/lib/imgproxy/options_casters/trim.rb +3 -1
  48. data/lib/imgproxy/options_casters/unsharp_masking.rb +25 -0
  49. data/lib/imgproxy/options_casters/video_thumbnail_tile.rb +34 -0
  50. data/lib/imgproxy/options_casters/watermark.rb +5 -4
  51. data/lib/imgproxy/options_casters/watermark_size.rb +21 -0
  52. data/lib/imgproxy/options_casters/{gif_options.rb → webp_options.rb} +6 -5
  53. data/lib/imgproxy/options_casters/zoom.rb +27 -0
  54. data/lib/imgproxy/service_config.rb +111 -0
  55. data/lib/imgproxy/trim_array.rb +2 -0
  56. data/lib/imgproxy/url_adapters/active_storage.rb +15 -3
  57. data/lib/imgproxy/url_adapters/shrine.rb +9 -3
  58. data/lib/imgproxy/url_adapters.rb +3 -0
  59. data/lib/imgproxy/url_builders/base.rb +184 -0
  60. data/lib/imgproxy/url_builders/info.rb +44 -0
  61. data/lib/imgproxy/url_builders/processing.rb +59 -0
  62. data/lib/imgproxy/version.rb +3 -1
  63. data/lib/imgproxy.rb +19 -55
  64. data/logo/logo-dark.svg +22 -0
  65. data/logo/logo-light.svg +31 -0
  66. metadata +74 -35
  67. data/lib/imgproxy/builder.rb +0 -140
  68. data/lib/imgproxy/options.rb +0 -115
  69. data/lib/imgproxy/options_aliases.rb +0 -43
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "yard"
5
+
6
+ module YARD
7
+ # Add some formatting to the GitHub alert tags since they are not supported
8
+ # by YARD
9
+ module GithubAlerts
10
+ def html_markup_markdown(text)
11
+ super(text.gsub(/\[!(IMPORTANT|TIP|NOTE)\]/) { "**#{Regexp.last_match(1)}:**" })
12
+ end
13
+ end
14
+
15
+ Templates::Template.extra_includes << GithubAlerts
16
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yard"
4
+
5
+ module YARD
6
+ # Fixes relative links in markdown files.
7
+ module RelativeMarkdownLinks
8
+ def resolve_links(text)
9
+ return super unless options.files
10
+
11
+ filenames = options.files.each_with_object({}) do |file, fns|
12
+ fn = file.filename
13
+ fns[fn] = fn
14
+ fns["#{fn.tr(".", "_")}.html"] = fn
15
+ end
16
+
17
+ # puts text
18
+
19
+ super(text.gsub(%r{<a href="([^"]+)"[^>]*>([^<]*)</a>}) do |str|
20
+ href = Regexp.last_match(1)
21
+ link_text = Regexp.last_match(2)
22
+
23
+ next str unless filenames.key?(href)
24
+
25
+ "{file:#{filenames[href]} #{link_text}}"
26
+ end)
27
+ end
28
+ end
29
+
30
+ Templates::Template.extra_includes << RelativeMarkdownLinks
31
+ end
@@ -1,28 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "anyway_config"
2
4
 
5
+ require "imgproxy/service_config"
3
6
  require "imgproxy/url_adapters"
4
7
 
5
8
  module Imgproxy
6
9
  # Imgproxy config
7
10
  #
8
- # @!attribute endpoint
9
- # imgproxy endpoint
10
- # @return [String]
11
- # @!attribute key
12
- # imgproxy hex-encoded signature key
13
- # @return [String]
14
- # @!attribute salt
15
- # imgproxy hex-encoded signature salt
16
- # @return [String]
17
- # @!attribute raw_key
18
- # Decoded signature key
19
- # @return [String]
20
- # @!attribute raw_salt
21
- # Decoded signature salt
22
- # @return [String]
23
- # @!attribute signature_size
24
- # imgproxy signature size. Defaults to 32
25
- # @return [String]
26
11
  # @!attribute use_short_options
27
12
  # Use short processing option names (+rs+ for +resize+, +g+ for +gravity+, etc).
28
13
  # Defaults to true
@@ -50,12 +35,6 @@ module Imgproxy
50
35
  # @see https://github.com/palkan/anyway_config anyway_config
51
36
  class Config < Anyway::Config
52
37
  attr_config(
53
- :endpoint,
54
- :key,
55
- :salt,
56
- :raw_key,
57
- :raw_salt,
58
- signature_size: 32,
59
38
  use_short_options: true,
60
39
  base64_encode_urls: false,
61
40
  always_escape_plain_urls: false,
@@ -63,36 +42,112 @@ module Imgproxy
63
42
  use_gcs_urls: false,
64
43
  gcs_bucket: nil,
65
44
  shrine_host: nil,
45
+ services: {},
46
+ )
47
+
48
+ coerce_types(
49
+ use_short_options: :boolean,
50
+ base64_encode_urls: :boolean,
51
+ always_escape_plain_urls: :boolean,
52
+ use_s3_urls: :boolean,
53
+ use_gcs_urls: :boolean,
54
+ gcs_bucket: :string,
55
+ shrine_host: :string,
66
56
  )
67
57
 
68
- alias_method :set_key, :key=
69
- alias_method :set_raw_key, :raw_key=
70
- alias_method :set_salt, :salt=
71
- alias_method :set_raw_salt, :raw_salt=
72
- private :set_key, :set_raw_key, :set_salt, :set_raw_salt
58
+ def endpoint
59
+ service(:default).endpoint
60
+ end
61
+
62
+ def endpoint=(value)
63
+ service(:default).endpoint = value
64
+ end
65
+
66
+ def key
67
+ service(:default).key
68
+ end
73
69
 
74
70
  def key=(value)
75
- value = value&.to_s
76
- super(value)
77
- set_raw_key(value && [value].pack("H*"))
71
+ service(:default).key = value
72
+ end
73
+
74
+ def raw_key
75
+ service(:default).raw_key
78
76
  end
79
77
 
80
78
  def raw_key=(value)
81
- value = value&.to_s
82
- super(value)
83
- set_key(value&.unpack("H*")&.first)
79
+ service(:default).raw_key = value
80
+ end
81
+
82
+ def salt
83
+ service(:default).salt
84
84
  end
85
85
 
86
86
  def salt=(value)
87
- value = value&.to_s
88
- super(value)
89
- set_raw_salt(value && [value].pack("H*"))
87
+ service(:default).salt = value
88
+ end
89
+
90
+ def raw_salt
91
+ service(:default).raw_salt
90
92
  end
91
93
 
92
94
  def raw_salt=(value)
93
- value = value&.to_s
94
- super(value)
95
- set_salt(value&.unpack("H*")&.first)
95
+ service(:default).raw_salt = value
96
+ end
97
+
98
+ def signature_size
99
+ service(:default).signature_size
100
+ end
101
+
102
+ def signature_size=(value)
103
+ service(:default).signature_size = value
104
+ end
105
+
106
+ def source_url_encryption_key
107
+ service(:default).source_url_encryption_key
108
+ end
109
+
110
+ def source_url_encryption_key=(value)
111
+ service(:default).source_url_encryption_key = value
112
+ end
113
+
114
+ def raw_source_url_encryption_key
115
+ service(:default).raw_source_url_encryption_key
116
+ end
117
+
118
+ def raw_source_url_encryption_key=(value)
119
+ service(:default).raw_source_url_encryption_key = value
120
+ end
121
+
122
+ def always_encrypt_source_urls
123
+ service(:default).always_encrypt_source_urls
124
+ end
125
+
126
+ def always_encrypt_source_urls=(value)
127
+ service(:default).always_encrypt_source_urls = value
128
+ end
129
+
130
+ def service(name)
131
+ services[name.to_sym] ||= services[:default].dup
132
+
133
+ yield services[name.to_sym] if block_given?
134
+
135
+ services[name.to_sym]
136
+ end
137
+
138
+ def services
139
+ @services ||= {}.tap do |s|
140
+ s[:default] = ServiceConfig.new
141
+
142
+ super.each do |name, data|
143
+ config = s[name.to_sym] = s[:default].dup
144
+
145
+ data.symbolize_keys.slice(*config.class.config_attributes).each do |key, value|
146
+ value = config.class.type_caster.coerce(key, value)
147
+ config.public_send("#{key}=", value)
148
+ end
149
+ end
150
+ end
96
151
  end
97
152
 
98
153
  # @deprecated Please use {#key} instead
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Imgproxy
2
4
  module Extensions
3
5
  # Extension for ActiveStorage
@@ -6,20 +8,20 @@ module Imgproxy
6
8
  # Returns imgproxy URL for an attachment
7
9
  #
8
10
  # @return [String]
9
- # @param options [Hash, Imgproxy::Builder]
11
+ # @param options [Hash, Imgproxy::UrlBuilders::Processing]
10
12
  # @see Imgproxy.url_for
11
13
  def imgproxy_url(options = {})
12
- return options.url_for(self) if options.is_a?(Imgproxy::Builder)
14
+ return options.url_for(self) if options.is_a?(Imgproxy::UrlBuilders::Processing)
13
15
  Imgproxy.url_for(self, options)
14
16
  end
15
17
 
16
18
  # Returns imgproxy info URL for an attachment
17
19
  #
18
20
  # @return [String]
19
- # @param options [Hash, Imgproxy::Builder]
21
+ # @param options [Hash, Imgproxy::UrlBuilders::Info]
20
22
  # @see Imgproxy.info_url_for
21
23
  def imgproxy_info_url(options = {})
22
- return options.info_url_for(self) if options.is_a?(Imgproxy::Builder)
24
+ return options.info_url_for(self) if options.is_a?(Imgproxy::UrlBuilders::Info)
23
25
  Imgproxy.info_url_for(self, options)
24
26
  end
25
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Imgproxy
2
4
  module Extensions
3
5
  # Extension for Shrine::UploadedFile
@@ -6,20 +8,20 @@ module Imgproxy
6
8
  # Returns imgproxy URL for a Shrine::UploadedFile instance
7
9
  #
8
10
  # @return [String]
9
- # @param options [Hash, Imgproxy::Builder]
11
+ # @param options [Hash, Imgproxy::UrlBuilders::Processing]
10
12
  # @see Imgproxy.url_for
11
13
  def imgproxy_url(options = {})
12
- return options.url_for(self) if options.is_a?(Imgproxy::Builder)
14
+ return options.url_for(self) if options.is_a?(Imgproxy::UrlBuilders::Processing)
13
15
  Imgproxy.url_for(self, options)
14
16
  end
15
17
 
16
18
  # Returns imgproxy info URL for a Shrine::UploadedFile instance
17
19
  #
18
20
  # @return [String]
19
- # @param options [Hash, Imgproxy::Builder]
21
+ # @param options [Hash, Imgproxy::UrlBuilders::Info]
20
22
  # @see Imgproxy.info_url_for
21
23
  def imgproxy_info_url(options = {})
22
- return options.info_url_for(self) if options.is_a?(Imgproxy::Builder)
24
+ return options.info_url_for(self) if options.is_a?(Imgproxy::UrlBuilders::Info)
23
25
  Imgproxy.info_url_for(self, options)
24
26
  end
25
27
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imgproxy
4
+ module OptionAliases
5
+ INFO = {
6
+ size: :s,
7
+ format: :f,
8
+ dimensions: :d,
9
+ video_meta: :vm,
10
+ detect_objects: :do,
11
+ colorspace: :cs,
12
+ bands: :b,
13
+ sample_format: :sf,
14
+ pages_number: :pn,
15
+ alpha: :a,
16
+ crop: :c,
17
+ palette: :p,
18
+ average: :avg,
19
+ dominant_colors: :dc,
20
+ blurhash: :bh,
21
+ calc_hashsum: :chs,
22
+ page: :pg,
23
+ video_thumbnail_second: :vts,
24
+ video_thumbnail_keyframes: :vtk,
25
+ cachebuster: :cb,
26
+ expires: :exp,
27
+ preset: :pr,
28
+ hashsum: :hs,
29
+ max_src_resolution: :msr,
30
+ max_src_file_size: :msfs,
31
+ }.freeze
32
+ end
33
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imgproxy
4
+ module OptionAliases
5
+ PROCESSING = {
6
+ resize: :rs,
7
+ size: :s,
8
+ resizing_type: :rt,
9
+ resizing_algorithm: :ra,
10
+ width: :w,
11
+ height: :h,
12
+ "min-width": :mw,
13
+ "min-height": :mh,
14
+ zoom: :z,
15
+ enlarge: :el,
16
+ extend: :ex,
17
+ extend_aspect_ratio: :exar,
18
+ gravity: :g,
19
+ crop: :c,
20
+ trim: :t,
21
+ padding: :pd,
22
+ auto_rotate: :ar,
23
+ rotate: :rot,
24
+ background: :bg,
25
+ background_alpha: :bga,
26
+ adjust: :a,
27
+ brightness: :br,
28
+ contrast: :co,
29
+ saturation: :sa,
30
+ blur: :bl,
31
+ sharpen: :sh,
32
+ pixelate: :pix,
33
+ unsharp_masking: :ush,
34
+ blur_detections: :bd,
35
+ draw_detections: :dd,
36
+ gradient: :gr,
37
+ watermark: :wm,
38
+ watermark_url: :wmu,
39
+ watermark_text: :wmt,
40
+ watermark_size: :wms,
41
+ watermark_shadow: :wmsh,
42
+ style: :st,
43
+ strip_metadata: :sm,
44
+ keep_copyright: :kcr,
45
+ strip_color_profile: :scp,
46
+ enforce_thumbnail: :eth,
47
+ quality: :q,
48
+ format_quality: :fq,
49
+ autoquality: :aq,
50
+ max_bytes: :mb,
51
+ jpeg_options: :jpego,
52
+ png_options: :pngo,
53
+ webp_options: :webpo,
54
+ page: :pg,
55
+ pages: :pgs,
56
+ disable_animation: :da,
57
+ video_thumbnail_second: :vts,
58
+ video_thumbnail_keyframes: :vtk,
59
+ video_thumbnail_tile: :vtt,
60
+ fallback_image_url: :fiu,
61
+ skip_processing: :skp,
62
+ cachebuster: :cb,
63
+ expires: :exp,
64
+ filename: :fn,
65
+ return_attachment: :att,
66
+ preset: :pr,
67
+ hashsum: :hs,
68
+ max_src_resolution: :msr,
69
+ max_src_file_size: :msfs,
70
+ max_animation_frames: :maf,
71
+ max_animation_frame_resolution: :mafr,
72
+ }.freeze
73
+ end
74
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "imgproxy/trim_array"
4
+
5
+ module Imgproxy
6
+ module OptionsBuilders
7
+ # Formats and regroups URL options
8
+ class Base < Hash
9
+ using TrimArray
10
+
11
+ # Hash of options casters. Redefine this in subclases
12
+ CASTERS = {}.freeze
13
+ # Array of meta-options names. Redefine this in subclases
14
+ META = [].freeze
15
+
16
+ # @param options [Hash] raw options
17
+ def initialize(options)
18
+ super()
19
+
20
+ # Options order hack: initialize known and meta options with nil value to preserve order
21
+ self.class::CASTERS.each_key do |n|
22
+ self[n] = nil if options.key?(n) || self.class::META.include?(n)
23
+ end
24
+
25
+ options.each do |name, value|
26
+ caster = self.class::CASTERS[name]
27
+ self[name] = caster ? caster.cast(value) : unwrap_hash(value)
28
+ end
29
+
30
+ group_opts
31
+
32
+ compact!
33
+ end
34
+
35
+ private
36
+
37
+ def unwrap_hash(raw)
38
+ return raw unless raw.is_a?(Hash)
39
+
40
+ raw.flat_map do |_key, val|
41
+ unwrap_hash(val)
42
+ end
43
+ end
44
+
45
+ def group_opts
46
+ # Do nothing in the base class. Can be redefined in subclasses
47
+ end
48
+
49
+ def extract_and_trim_nils(*keys)
50
+ keys.map { |k| delete(k) }.trim!
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "imgproxy/options_builders/base"
4
+
5
+ require "imgproxy/options_casters/string"
6
+ require "imgproxy/options_casters/integer"
7
+ require "imgproxy/options_casters/float"
8
+ require "imgproxy/options_casters/bool"
9
+ require "imgproxy/options_casters/array"
10
+ require "imgproxy/options_casters/alpha"
11
+ require "imgproxy/options_casters/crop"
12
+ require "imgproxy/options_casters/average"
13
+ require "imgproxy/options_casters/dominant_colors"
14
+ require "imgproxy/options_casters/blurhash"
15
+ require "imgproxy/options_casters/hashsum"
16
+
17
+ module Imgproxy
18
+ module OptionsBuilders
19
+ # Formats and regroups info options
20
+ class Info < Base
21
+ CASTERS = {
22
+ size: Imgproxy::OptionsCasters::Bool,
23
+ format: Imgproxy::OptionsCasters::Bool,
24
+ dimensions: Imgproxy::OptionsCasters::Bool,
25
+ exif: Imgproxy::OptionsCasters::Bool,
26
+ iptc: Imgproxy::OptionsCasters::Bool,
27
+ video_meta: Imgproxy::OptionsCasters::Bool,
28
+ detect_objects: Imgproxy::OptionsCasters::Bool,
29
+ colorspace: Imgproxy::OptionsCasters::Bool,
30
+ bands: Imgproxy::OptionsCasters::Bool,
31
+ sample_format: Imgproxy::OptionsCasters::Bool,
32
+ pages_number: Imgproxy::OptionsCasters::Bool,
33
+ alpha: Imgproxy::OptionsCasters::Alpha,
34
+ crop: Imgproxy::OptionsCasters::Crop,
35
+ palette: Imgproxy::OptionsCasters::Integer,
36
+ average: Imgproxy::OptionsCasters::Average,
37
+ dominant_colors: Imgproxy::OptionsCasters::DominantColors,
38
+ blurhash: Imgproxy::OptionsCasters::Blurhash,
39
+ calc_hashsum: Imgproxy::OptionsCasters::Array,
40
+ page: Imgproxy::OptionsCasters::Integer,
41
+ video_thumbnail_second: Imgproxy::OptionsCasters::Integer,
42
+ video_thumbnail_keyframes: Imgproxy::OptionsCasters::Bool,
43
+ cachebuster: Imgproxy::OptionsCasters::String,
44
+ expires: Imgproxy::OptionsCasters::Integer,
45
+ preset: Imgproxy::OptionsCasters::Array,
46
+ hashsum: Imgproxy::OptionsCasters::Hashsum,
47
+ max_src_resolution: Imgproxy::OptionsCasters::Float,
48
+ max_src_file_size: Imgproxy::OptionsCasters::Integer,
49
+ }.freeze
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "imgproxy/options_builders/base"
4
+
5
+ require "imgproxy/options_casters/string"
6
+ require "imgproxy/options_casters/integer"
7
+ require "imgproxy/options_casters/float"
8
+ require "imgproxy/options_casters/bool"
9
+ require "imgproxy/options_casters/array"
10
+ require "imgproxy/options_casters/base64"
11
+ require "imgproxy/options_casters/resize"
12
+ require "imgproxy/options_casters/size"
13
+ require "imgproxy/options_casters/zoom"
14
+ require "imgproxy/options_casters/extend"
15
+ require "imgproxy/options_casters/gravity"
16
+ require "imgproxy/options_casters/crop"
17
+ require "imgproxy/options_casters/trim"
18
+ require "imgproxy/options_casters/padding"
19
+ require "imgproxy/options_casters/background"
20
+ require "imgproxy/options_casters/adjust"
21
+ require "imgproxy/options_casters/unsharp_masking"
22
+ require "imgproxy/options_casters/blur_detections"
23
+ require "imgproxy/options_casters/draw_detections"
24
+ require "imgproxy/options_casters/gradient"
25
+ require "imgproxy/options_casters/watermark"
26
+ require "imgproxy/options_casters/watermark_size"
27
+ require "imgproxy/options_casters/format_quality"
28
+ require "imgproxy/options_casters/autoquality"
29
+ require "imgproxy/options_casters/jpeg_options"
30
+ require "imgproxy/options_casters/png_options"
31
+ require "imgproxy/options_casters/webp_options"
32
+ require "imgproxy/options_casters/video_thumbnail_tile"
33
+ require "imgproxy/options_casters/filename"
34
+ require "imgproxy/options_casters/hashsum"
35
+
36
+ module Imgproxy
37
+ module OptionsBuilders
38
+ # Formats and regroups processing options
39
+ class Processing < Base
40
+ CASTERS = {
41
+ resize: Imgproxy::OptionsCasters::Resize,
42
+ size: Imgproxy::OptionsCasters::Size,
43
+ resizing_type: Imgproxy::OptionsCasters::String,
44
+ resizing_algorithm: Imgproxy::OptionsCasters::String,
45
+ width: Imgproxy::OptionsCasters::Integer,
46
+ height: Imgproxy::OptionsCasters::Integer,
47
+ "min-width": Imgproxy::OptionsCasters::Integer,
48
+ "min-height": Imgproxy::OptionsCasters::Integer,
49
+ zoom: Imgproxy::OptionsCasters::Zoom,
50
+ dpr: Imgproxy::OptionsCasters::Float,
51
+ enlarge: Imgproxy::OptionsCasters::Bool,
52
+ extend: Imgproxy::OptionsCasters::Extend,
53
+ extend_aspect_ratio: Imgproxy::OptionsCasters::Extend,
54
+ gravity: Imgproxy::OptionsCasters::Gravity,
55
+ crop: Imgproxy::OptionsCasters::Crop,
56
+ trim: Imgproxy::OptionsCasters::Trim,
57
+ padding: Imgproxy::OptionsCasters::Padding,
58
+ auto_rotate: Imgproxy::OptionsCasters::Bool,
59
+ rotate: Imgproxy::OptionsCasters::Integer,
60
+ background: Imgproxy::OptionsCasters::Background,
61
+ background_alpha: Imgproxy::OptionsCasters::Float,
62
+ adjust: Imgproxy::OptionsCasters::Adjust,
63
+ brightness: Imgproxy::OptionsCasters::Integer,
64
+ contrast: Imgproxy::OptionsCasters::Float,
65
+ saturation: Imgproxy::OptionsCasters::Float,
66
+ blur: Imgproxy::OptionsCasters::Float,
67
+ sharpen: Imgproxy::OptionsCasters::Float,
68
+ pixelate: Imgproxy::OptionsCasters::Integer,
69
+ unsharp_masking: Imgproxy::OptionsCasters::UnsharpMasking,
70
+ blur_detections: Imgproxy::OptionsCasters::BlurDetections,
71
+ draw_detections: Imgproxy::OptionsCasters::DrawDetections,
72
+ gradient: Imgproxy::OptionsCasters::Gradient,
73
+ watermark: Imgproxy::OptionsCasters::Watermark,
74
+ watermark_url: Imgproxy::OptionsCasters::Base64,
75
+ watermark_text: Imgproxy::OptionsCasters::Base64,
76
+ watermark_size: Imgproxy::OptionsCasters::WatermarkSize,
77
+ watermark_shadow: Imgproxy::OptionsCasters::Float,
78
+ style: Imgproxy::OptionsCasters::Base64,
79
+ strip_metadata: Imgproxy::OptionsCasters::Bool,
80
+ keep_copyright: Imgproxy::OptionsCasters::Bool,
81
+ dpi: Imgproxy::OptionsCasters::Float,
82
+ strip_color_profile: Imgproxy::OptionsCasters::Bool,
83
+ enforce_thumbnail: Imgproxy::OptionsCasters::Bool,
84
+ quality: Imgproxy::OptionsCasters::Integer,
85
+ format_quality: Imgproxy::OptionsCasters::FormatQuality,
86
+ autoquality: Imgproxy::OptionsCasters::Autoquality,
87
+ max_bytes: Imgproxy::OptionsCasters::Integer,
88
+ jpeg_options: Imgproxy::OptionsCasters::JpegOptions,
89
+ png_options: Imgproxy::OptionsCasters::PngOptions,
90
+ webp_options: Imgproxy::OptionsCasters::WebpOptions,
91
+ format: Imgproxy::OptionsCasters::String,
92
+ page: Imgproxy::OptionsCasters::Integer,
93
+ pages: Imgproxy::OptionsCasters::Integer,
94
+ disable_animation: Imgproxy::OptionsCasters::Bool,
95
+ video_thumbnail_second: Imgproxy::OptionsCasters::Integer,
96
+ video_thumbnail_keyframes: Imgproxy::OptionsCasters::Bool,
97
+ video_thumbnail_tile: Imgproxy::OptionsCasters::VideoThumbnailTile,
98
+ fallback_image_url: Imgproxy::OptionsCasters::Base64,
99
+ skip_processing: Imgproxy::OptionsCasters::Array,
100
+ raw: Imgproxy::OptionsCasters::Bool,
101
+ cachebuster: Imgproxy::OptionsCasters::String,
102
+ expires: Imgproxy::OptionsCasters::Integer,
103
+ filename: Imgproxy::OptionsCasters::Filename,
104
+ return_attachment: Imgproxy::OptionsCasters::Bool,
105
+ preset: Imgproxy::OptionsCasters::Array,
106
+ hashsum: Imgproxy::OptionsCasters::Hashsum,
107
+ max_src_resolution: Imgproxy::OptionsCasters::Float,
108
+ max_src_file_size: Imgproxy::OptionsCasters::Integer,
109
+ max_animation_frames: Imgproxy::OptionsCasters::Integer,
110
+ max_animation_frame_resolution: Imgproxy::OptionsCasters::Float,
111
+ }.freeze
112
+
113
+ META = %i[size resize adjust].freeze
114
+
115
+ def initialize(options)
116
+ super
117
+
118
+ # Legacy name for unsharp_masking
119
+ if options.key?(:unsharpening)
120
+ warn "[DEPRECATION] `unsharpening` option is deprecated. Please use `unsharp_masking` instead."
121
+ delete(:unsharpening)
122
+ unless key?(:unsharp_masking)
123
+ self[:unsharp_masking] = CASTERS[:unsharp_masking].cast(options[:unsharpening])
124
+ end
125
+ end
126
+ end
127
+
128
+ private
129
+
130
+ def group_opts
131
+ group_resizing_opts
132
+ group_adjust_opts
133
+ end
134
+
135
+ def group_resizing_opts
136
+ return unless self[:width] && self[:height] && !self[:size] && !self[:resize]
137
+
138
+ self[:size] = extract_and_trim_nils(:width, :height, :enlarge, :extend)
139
+ self[:resize] = [delete(:resizing_type), *delete(:size)] if self[:resizing_type]
140
+ end
141
+
142
+ def group_adjust_opts
143
+ return if self[:adjust]
144
+ return unless values_at(:brightness, :contrast, :saturation).count { |o| o } > 1
145
+
146
+ self[:adjust] = extract_and_trim_nils(:brightness, :contrast, :saturation)
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "imgproxy/options_casters/group"
2
4
  require "imgproxy/options_casters/integer"
3
5
  require "imgproxy/options_casters/float"
4
6
 
5
7
  module Imgproxy
6
8
  module OptionsCasters
7
- # Casts gravity option
9
+ # Casts `adjust` processing option
8
10
  module Adjust
9
11
  CASTER = Imgproxy::OptionsCasters::Group.new(
10
12
  brightness: Imgproxy::OptionsCasters::Integer,
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "imgproxy/options_casters/group"
4
+ require "imgproxy/options_casters/bool"
5
+
6
+ module Imgproxy
7
+ module OptionsCasters
8
+ # Casts `alpha` info option
9
+ module Alpha
10
+ CASTER = Imgproxy::OptionsCasters::Group.new(
11
+ alpha: Imgproxy::OptionsCasters::Bool,
12
+ check_transparency: Imgproxy::OptionsCasters::Bool,
13
+ ).freeze
14
+
15
+ def self.cast(raw)
16
+ # Allow alpha to be just a boolean
17
+ return Imgproxy::OptionsCasters::Bool.cast(raw) if [true, false].include?(raw)
18
+
19
+ return raw unless raw.is_a?(Hash)
20
+ return if raw[:alpha].nil?
21
+
22
+ values = CASTER.cast(raw)
23
+ values[0].zero? ? 0 : values
24
+ end
25
+ end
26
+ end
27
+ end