Class: Thumbor::CryptoURL
- Inherits:
-
Object
- Object
- Thumbor::CryptoURL
- Defined in:
- lib/thumbor/crypto_url.rb
Instance Method Summary collapse
- #calculate_centered_crop(options) ⇒ Object
- #calculate_width_and_height(url_parts, options) ⇒ Object
- #generate(options) ⇒ Object
-
#initialize(key = nil) ⇒ CryptoURL
constructor
A new instance of CryptoURL.
- #url_for(options) ⇒ Object
- #url_safe_base64(str) ⇒ Object
Constructor Details
#initialize(key = nil) ⇒ CryptoURL
Returns a new instance of CryptoURL.
10 11 12 |
# File 'lib/thumbor/crypto_url.rb', line 10 def initialize(key = nil) @key = key end |
Instance Method Details
#calculate_centered_crop(options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/thumbor/crypto_url.rb', line 40 def calculate_centered_crop() width = [:width] height = [:height] original_width = [:original_width] original_height = [:original_height] center = [:center] return unless original_width && original_height && center && (width || height) raise 'center must be an array of x,y' unless center.is_a?(Array) && center.length == 2 center_x, center_y = center width ||= original_width height ||= original_height width = width.abs height = height.abs new_aspect_ratio = width / height.to_f original_aspect_ratio = original_width / original_height.to_f crop = nil # We're going wider, vertical crop if new_aspect_ratio > original_aspect_ratio # How tall should we be? because new_aspect_ratio is > original_aspect_ratio we can be sure # that cropped_height is always less than original_height (original). This is assumed below. cropped_height = (original_width / new_aspect_ratio).round # Calculate coordinates around center top_crop_point = (center_y - (cropped_height * 0.5)).round bottom_crop_point = (center_y + (cropped_height * 0.5)).round # If we've gone above the top of the image, take all from the bottom if top_crop_point.negative? top_crop_point = 0 bottom_crop_point = cropped_height # If we've gone below the top of the image, take all from the top elsif bottom_crop_point > original_height top_crop_point = original_height - cropped_height bottom_crop_point = original_height # Because cropped_height < original_height, top_crop_point and # bottom_crop_point will never both be out of bounds end # Put it together crop = [0, top_crop_point, original_width, bottom_crop_point] # We're going taller, horizontal crop elsif new_aspect_ratio < original_aspect_ratio # How wide should we be? because new_aspect_ratio is < original_aspect_ratio we can be sure # that cropped_width is always less than original_width (original). This is assumed below. cropped_width = (original_height * new_aspect_ratio).round # Calculate coordinates around center left_crop_point = (center_x - (cropped_width * 0.5)).round right_crop_point = (center_x + (cropped_width * 0.5)).round # If we've gone beyond the left of the image, take all from the right if left_crop_point.negative? left_crop_point = 0 right_crop_point = cropped_width # If we've gone beyond the right of the image, take all from the left elsif right_crop_point > original_width left_crop_point = original_width - cropped_width right_crop_point = original_width # Because cropped_width < original_width, left_crop_point and # right_crop_point will never both be out of bounds end # Put it together crop = [left_crop_point, 0, right_crop_point, original_height] end [:crop] = crop end |
#calculate_width_and_height(url_parts, options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/thumbor/crypto_url.rb', line 14 def calculate_width_and_height(url_parts, ) width = [:width] height = [:height] width *= -1 if width && [:flip] height *= -1 if height && [:flop] if width || height width ||= 0 height ||= 0 end has_width = width has_height = height if [:flip] && !has_width && !has_height width = '-0' height = '0' if !has_height && !([:flop]) end if [:flop] && !has_width && !has_height height = '-0' width = '0' if !has_width && !([:flip]) end url_parts.push("#{width}x#{height}") if width || height end |
#generate(options) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/thumbor/crypto_url.rb', line 175 def generate() thumbor_path = [] = url_for() thumbor_path << "#{}/" unless .empty? thumbor_path << [:image] if @key.nil? thumbor_path.insert(0, '/unsafe/') else signature = url_safe_base64(OpenSSL::HMAC.digest('sha1', @key, thumbor_path.join)) thumbor_path.insert(0, "/#{signature}/") end thumbor_path.join end |
#url_for(options) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/thumbor/crypto_url.rb', line 114 def url_for() raise 'image is a required argument.' unless [:image] url_parts = [] url_parts.push('debug') if [:debug] if [:trim] = ['trim'] << [:trim] unless ([:trim] == true) || ([:trim][0] == true) url_parts.push(.join(':')) end url_parts.push('meta') if [:meta] calculate_centered_crop() crop = [:crop] if crop crop_left = crop[0] crop_top = crop[1] crop_right = crop[2] crop_bottom = crop[3] if crop_left.positive? || crop_top.positive? || crop_bottom.positive? || crop_right.positive? url_parts.push(crop_left.to_s << 'x' << crop_top.to_s << ':' << crop_right.to_s << 'x' << crop_bottom.to_s) end end %i[fit_in adaptive_fit_in full_fit_in adaptive_full_fit_in].each do |fit| url_parts.push(fit.to_s.gsub('_', '-')) if [fit] end if (.include?(:fit_in) || .include?(:full_fit_in)) && !(.include?(:width) || .include?(:height)) raise ArgumentError, 'When using fit-in or full-fit-in, you must specify width and/or height.' end calculate_width_and_height(url_parts, ) url_parts.push([:halign]) if [:halign] && ([:halign] != :center) url_parts.push([:valign]) if [:valign] && ([:valign] != :middle) url_parts.push('smart') if [:smart] if [:filters] && ![:filters].empty? filter_parts = [] [:filters].each do |filter| filter_parts.push(filter) end url_parts.push("filters:#{filter_parts.join(':')}") end url_parts.join('/') end |
#url_safe_base64(str) ⇒ Object
171 172 173 |
# File 'lib/thumbor/crypto_url.rb', line 171 def url_safe_base64(str) Base64.encode64(str).gsub('+', '-').gsub('/', '_').gsub!(/\n/, '') end |