Class: Thumbor::CryptoURL
- Inherits:
-
Object
- Object
- Thumbor::CryptoURL
- Defined in:
- lib/thumbor/crypto_url.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #calculate_centered_crop(options) ⇒ Object
- #calculate_width_and_height(url_parts, options) ⇒ Object
- #generate(options) ⇒ Object
-
#initialize(key = false) ⇒ CryptoURL
constructor
A new instance of CryptoURL.
- #url_for(options) ⇒ Object
- #url_safe_base64(str) ⇒ Object
Constructor Details
#initialize(key = false) ⇒ CryptoURL
Returns a new instance of CryptoURL.
10 11 12 |
# File 'lib/thumbor/crypto_url.rb', line 10 def initialize(key=false) @key = key end |
Instance Attribute Details
#computed_key ⇒ Object
14 15 16 |
# File 'lib/thumbor/crypto_url.rb', line 14 def computed_key (@key * 16)[0..15] end |
Instance Method Details
#calculate_centered_crop(options) ⇒ Object
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 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/thumbor/crypto_url.rb', line 48 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) unless center.kind_of?(Array) && center.length == 2 raise 'center must be an array of x,y' end 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 < 0 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 < 0 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/thumbor/crypto_url.rb', line 18 def calculate_width_and_height(url_parts, ) width = [:width] height = [:height] if width and [:flip] width = width * -1 end if height and [:flop] height = height * -1 end if width or height width = 0 if not width height = 0 if not height end has_width = width has_height = height if [:flip] and not has_width and not has_height width = "-0" height = '0' if not has_height and not [:flop] end if [:flop] and not has_width and not has_height height = "-0" width = '0' if not has_width and not [:flip] end url_parts.push("#{width}x#{height}") if width || height end |
#generate(options) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/thumbor/crypto_url.rb', line 199 def generate() thumbor_path = "" = url_for() thumbor_path << + '/' unless .empty? thumbor_path << [:image] if @key signature = url_safe_base64(OpenSSL::HMAC.digest('sha1', @key, thumbor_path)) thumbor_path.insert(0, "/#{signature}/") else thumbor_path.insert(0, "/unsafe/") end thumbor_path end |
#url_for(options) ⇒ Object
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/thumbor/crypto_url.rb', line 124 def url_for() if not [:image] raise 'image is a required argument.' end url_parts = Array.new if [:debug] url_parts.push('debug') end if [:trim] = ['trim'] << [:trim] unless [:trim] == true or [:trim][0] == true url_parts.push(.join(':')) end if [:meta] url_parts.push('meta') end 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 > 0 or crop_top > 0 or crop_bottom > 0 or crop_right > 0 url_parts.push(crop_left.to_s << 'x' << crop_top.to_s << ':' << crop_right.to_s << 'x' << crop_bottom.to_s) end end [:fit_in, :adaptive_fit_in, :full_fit_in, :adaptive_full_fit_in].each do |fit| if [fit] url_parts.push(fit.to_s.gsub('_','-')) end end if .include?(:fit_in) or .include?(:full_fit_in) and not (.include?(:width) or .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, ) if [:halign] and [:halign] != :center url_parts.push([:halign]) end if [:valign] and [:valign] != :middle url_parts.push([:valign]) end if [:smart] url_parts.push('smart') end if [:filters] && ![:filters].empty? filter_parts = [] [:filters].each do |filter| filter_parts.push(filter) end url_parts.push("filters:#{ filter_parts.join(':') }") end return url_parts.join('/') end |
#url_safe_base64(str) ⇒ Object
195 196 197 |
# File 'lib/thumbor/crypto_url.rb', line 195 def url_safe_base64(str) Base64.encode64(str).gsub('+', '-').gsub('/', '_').gsub!(/[\n]/, '') end |