Method: Cloudinary::Utils.generate_transformation_string
- Defined in:
- lib/cloudinary/utils.rb
.generate_transformation_string(options = {}, allow_implicit_crop_mode = false) ⇒ Object
Warning: options are being destructively updated!
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/cloudinary/utils.rb', line 188 def self.generate_transformation_string(={}, allow_implicit_crop_mode = false) # allow_implicit_crop_mode was added to support height and width parameters without specifying a crop mode. # This only apply to this (cloudinary_gem) SDK if .is_a?(Array) return .map{|base_transformation| generate_transformation_string(base_transformation.clone, allow_implicit_crop_mode)}.reject(&:blank?).join("/") end symbolize_keys!() responsive_width = config_option_consume(, :responsive_width) size = .delete(:size) [:width], [:height] = size.split("x") if size width = [:width] width = width.to_s if width.is_a?(Symbol) height = [:height] has_layer = [:overlay].present? || [:underlay].present? crop = .delete(:crop) angle = build_array(.delete(:angle)).join(".") no_html_sizes = has_layer || angle.present? || crop.to_s == "fit" || crop.to_s == "limit" || crop.to_s == "lfill" .delete(:width) if width && (width.to_f < 1 || no_html_sizes || width.to_s.start_with?("auto") || responsive_width) .delete(:height) if height && (height.to_f < 1 || no_html_sizes || responsive_width) width=height=nil if crop.nil? && !has_layer && !width.to_s.start_with?("auto") && !allow_implicit_crop_mode background = .delete(:background) background = background.sub(/^#/, 'rgb:') if background color = .delete(:color) color = color.sub(/^#/, 'rgb:') if color base_transformations = build_array(.delete(:transformation)) if base_transformations.any?{|base_transformation| base_transformation.is_a?(Hash)} base_transformations = base_transformations.map do |base_transformation| base_transformation.is_a?(Hash) ? generate_transformation_string(base_transformation.clone, allow_implicit_crop_mode) : generate_transformation_string({:transformation=>base_transformation}, allow_implicit_crop_mode) end else named_transformation = base_transformations.join(".") base_transformations = [] end effect = .delete(:effect) effect = Array(effect).flatten.join(":") if effect.is_a?(Array) || effect.is_a?(Hash) border = .delete(:border) if border.is_a?(Hash) border = "#{border[:width] || 2}px_solid_#{(border[:color] || "black").sub(/^#/, 'rgb:')}" elsif border.to_s =~ /^\d+$/ # fallback to html border attribute [:border] = border border = nil end flags = build_array(.delete(:flags)).join(".") dpr = config_option_consume(, :dpr) if .include? :offset [:start_offset], [:end_offset] = split_range .delete(:offset) end fps = .delete(:fps) fps = fps.join('-') if fps.is_a? Array = process_layer(.delete(:overlay)) underlay = process_layer(.delete(:underlay)) ifValue = process_if(.delete(:if)) custom_function = process_custom_function(.delete(:custom_function)) custom_pre_function = process_custom_pre_function(.delete(:custom_pre_function)) params = { :a => normalize_expression(angle), :ar => normalize_expression(.delete(:aspect_ratio)), :b => background, :bo => border, :c => crop, :co => color, :dpr => normalize_expression(dpr), :e => normalize_expression(effect), :fl => flags, :fn => custom_function || custom_pre_function, :fps => fps, :h => normalize_expression(height), :l => , :o => normalize_expression(.delete(:opacity)), :q => normalize_expression(.delete(:quality)), :r => process_radius(.delete(:radius)), :t => named_transformation, :u => underlay, :w => normalize_expression(width), :x => normalize_expression(.delete(:x)), :y => normalize_expression(.delete(:y)), :z => normalize_expression(.delete(:zoom)) } SIMPLE_TRANSFORMATION_PARAMS.each do |param, option| params[param] = .delete(option) end params[:vc] = process_video_params params[:vc] if params[:vc].present? [:so, :eo, :du].each do |range_value| params[range_value] = norm_range_value params[range_value] if params[range_value].present? end variables = .delete(:variables) var_params = [] .each_pair do |key, value| if key =~ /^\$/ var_params.push "#{key}_#{normalize_expression(value.to_s)}" end end var_params.sort! unless variables.nil? || variables.empty? for name, value in variables var_params.push "#{name}_#{normalize_expression(value.to_s)}" end end variables = var_params.join(',') raw_transformation = .delete(:raw_transformation) transformation = params.reject{|_k,v| v.blank?}.map{|k,v| "#{k}_#{v}"}.sort transformation = transformation.join(",") transformation = [ifValue, variables, transformation, raw_transformation].reject(&:blank?).join(",") transformations = base_transformations << transformation if responsive_width responsive_width_transformation = Cloudinary.config.responsive_width_transformation || DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION transformations << generate_transformation_string(responsive_width_transformation.clone, allow_implicit_crop_mode) end if width.to_s.start_with?( "auto") || responsive_width [:responsive] = true end if dpr.to_s == "auto" [:hidpi] = true end transformations.reject(&:blank?).join("/") end |