Class: Morandi::ImageProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/morandi/image_processor.rb

Overview

ImageProcessor transforms an image.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, user_options, local_options = {}) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/morandi/image_processor.rb', line 16

def initialize(file, user_options, local_options = {})
  @file = file

  user_options.keys.grep(/^path/).each { |k| user_options.delete(k) }

  # Give priority to user_options
  @options = (local_options || {}).merge(user_options || {})
  @local_options = local_options

  @max_size_px = @options['output.max']
  @width = @options['output.width']
  @height = @options['output.height']
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/morandi/image_processor.rb', line 14

def options
  @options
end

#pbObject (readonly)

Returns the value of attribute pb.



14
15
16
# File 'lib/morandi/image_processor.rb', line 14

def pb
  @pb
end

Instance Method Details

#process!Object



30
31
32
33
34
35
36
37
38
39
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
# File 'lib/morandi/image_processor.rb', line 30

def process!
  case @file
  when String
    get_pixbuf
  when GdkPixbuf::Pixbuf, Morandi::ProfiledPixbuf
    @pb = @file
    @scale = 1.0
  end

  # Apply Red-Eye corrections
  apply_redeye!

  # Apply contrast, brightness etc
  apply_colour_manipulations!

  # apply rotation
  apply_rotate!

  # apply crop
  apply_crop!

  # apply filter
  apply_filters!

  # add border
  apply_decorations!

  @pb = @pb.scale_max([@width, @height].max) if @options['output.limit'] && @width && @height

  @pb
rescue GdkPixbuf::PixbufError::UnknownType => e
  raise UnknownTypeError, e.message
rescue GdkPixbuf::PixbufError::CorruptImage => e
  raise CorruptImageError, e.message
end

#resultObject

Returns generated pixbuf



67
68
69
70
# File 'lib/morandi/image_processor.rb', line 67

def result
  process! unless @pb
  @pb
end

#write_to_jpeg(write_to, quality = nil) ⇒ Object



84
85
86
87
# File 'lib/morandi/image_processor.rb', line 84

def write_to_jpeg(write_to, quality = nil)
  quality ||= options.fetch('quality', '97')
  @pb.save(write_to, 'jpeg', quality: quality.to_s)
end

#write_to_png(write_to, orientation = :any) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/morandi/image_processor.rb', line 72

def write_to_png(write_to, orientation = :any)
  pb = @pb

  case orientation
  when :landscape
    pb = @pb.rotate(90) if @pb.width < @pb.height
  when :portrait
    pb = @pb.rotate(90) if @pb.width > @pb.height
  end
  pb.save(write_to, 'png')
end