Class: FeaturedImage::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/featuredimage/converter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, opts = {}) ⇒ Converter

Returns a new instance of Converter.



16
17
18
19
20
21
22
23
24
25
# File 'lib/featuredimage/converter.rb', line 16

def initialize(width, height, opts = {})
	@width, @height = width, height

	# default convert options
	@opts = {
		:format => "JPG",
		:quality => 60
	}
	@opts.merge!(opts)
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/featuredimage/converter.rb', line 9

def height
  @height
end

#optsObject

Returns the value of attribute opts.



9
10
11
# File 'lib/featuredimage/converter.rb', line 9

def opts
  @opts
end

#widthObject

Returns the value of attribute width.



9
10
11
# File 'lib/featuredimage/converter.rb', line 9

def width
  @width
end

Class Method Details

.convert(image, w, h, opts = {}) ⇒ Object



11
12
13
14
# File 'lib/featuredimage/converter.rb', line 11

def self.convert(image, w, h, opts = {})
	converter = Converter.new(w, h, opts)
	converter.convert(image)
end

Instance Method Details

#convert(image) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/featuredimage/converter.rb', line 27

def convert(image)
	target_size = {w:@width, h:@height}
	original_size = {w:image.columns, h:image.rows}

	resized_size = get_resized_size(original_size, target_size)
	resized_image = image.resize(resized_size[:w], resized_size[:h])

	offset = get_offset(target_size, resized_size)
	cropped_image = resized_image.crop(offset[:x], offset[:y], target_size[:w], target_size[:h])

	image_to_blob(cropped_image)
end