Module: Scene7::Crop

Defined in:
lib/scene7/crop.rb

Class Method Summary collapse

Class Method Details

.convert_from_scale_first_and_format(params, additional_params = {}) ⇒ Object



5
6
7
# File 'lib/scene7/crop.rb', line 5

def self.convert_from_scale_first_and_format(params, additional_params = {})
  format_url_params convert_params_from_scale_first(params), additional_params
end

.convert_params_from_scale_first(orig_params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/scene7/crop.rb', line 17

def self.convert_params_from_scale_first(orig_params)
  {}.tap do |params|
    scale_factor = orig_params[:scale_factor].to_f
    params[:scale_factor] = 1.0 / scale_factor
    params[:height] = (orig_params[:height].to_f / scale_factor).round
    params[:width] = (orig_params[:width].to_f / scale_factor).round
    params[:x] = (orig_params[:x].to_f / scale_factor).round
    params[:y] = (orig_params[:y].to_f / scale_factor).round
    params[:quality] = orig_params[:quality] || 95
  end
end

.format_url_params(params, additional_params = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/scene7/crop.rb', line 9

def self.format_url_params(params, additional_params = {})
  require 'cgi' unless defined?(CGI) && defined?(CGI::escape)

  query = "scl=#{params.delete(:scale_factor)}&crop=#{params.delete(:x).round},#{params.delete(:y).round},#{params.delete(:width).round},#{params.delete(:height).round}&qlt=#{params.delete(:quality)}"
  query << "&" + additional_params.map {|k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&') if additional_params.length > 0
  query
end