Class: InsanoImageResizer::Processor

Inherits:
Object
  • Object
show all
Includes:
Cocaine, Configurable, Loggable
Defined in:
lib/insano_image_resizer/processor.rb

Constant Summary collapse

DEFAULT_QUALITY_LIMITS =
{ :min_area => { :area => 4000, :quality => 90 },
:max_area => { :area => 1000000, :quality => 60 }}

Instance Attribute Summary

Attributes included from Loggable

#log_object

Instance Method Summary collapse

Methods included from Loggable

#log, #log=, #use_same_log_as

Methods included from Configurable

included

Constructor Details

#initialize(options = {}) ⇒ Processor

Returns a new instance of Processor.



15
16
17
18
# File 'lib/insano_image_resizer/processor.rb', line 15

def initialize(options = {})
  @vips_path = options[:vips_path] || 'vips'
  @identify_path = options[:identify_path] || 'identify'
end

Instance Method Details

#process(input_path, viewport_size = {}, interest_point = {}, quality_limits = DEFAULT_QUALITY_LIMITS) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/insano_image_resizer/processor.rb', line 20

def process(input_path, viewport_size = {}, interest_point = {}, quality_limits=DEFAULT_QUALITY_LIMITS)
  width, height, original_format, target_extension = fetch_image_properties(input_path)

  exif_result = handle_exif_rotation(input_path, original_format)
  width, height = [height, width] if exif_result == :swap_dimensions

  output_tmp = Tempfile.new(['img', ".#{target_extension}"])

  transform = calculate_transform(input_path, width, height, viewport_size, interest_point)
  quality = target_jpg_quality(transform[:w], transform[:h], quality_limits) if target_extension == 'jpg'
  run_transform(input_path, output_tmp.path, transform, original_format, target_extension, quality)

  output_tmp
end

#target_jpg_quality(width, height, limits) ⇒ Object

limits is of the form: { :min_area => { :area => 4000, :quality => 90 },

:max_area => { :area => 1000000, :quality => 70 }}


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/insano_image_resizer/processor.rb', line 38

def target_jpg_quality(width, height, limits)
  return limits.to_i unless limits.is_a?(Hash)
  min_area = limits[:min_area][:area]
  min_area_quality = limits[:min_area][:quality]
  max_area = limits[:max_area][:area]
  max_area_quality = limits[:max_area][:quality]
  normalized_target_area = [width * height - min_area, 0].max
  normalized_max_area =  max_area - min_area
  target_area_fraction = [normalized_target_area.to_f / normalized_max_area, 1].min
  quality_span = min_area_quality - max_area_quality
  quality_fraction = quality_span * target_area_fraction
  min_area_quality - quality_fraction
end