Class: Mangos::Thumbnailer

Inherits:
Object
  • Object
show all
Defined in:
lib/mangos/thumbnailer.rb

Constant Summary collapse

PREVIEW_WIDTH =

PREVIEW_WIDTH = 211 PREVIEW_HEIGHT = 332

197
PREVIEW_HEIGHT =
310
PREVIEW_SMALL_WIDTH =
98
PREVIEW_SMALL_HEIGHT =
154

Instance Method Summary collapse

Constructor Details

#initialize(image_path, thumbnail_path) ⇒ Thumbnailer

Returns a new instance of Thumbnailer.



10
11
12
13
# File 'lib/mangos/thumbnailer.rb', line 10

def initialize(image_path, thumbnail_path)
  @image_path = image_path
  @thumbnail_path = thumbnail_path
end

Instance Method Details

#generateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mangos/thumbnailer.rb', line 15

def generate
  return if @thumbnail_path.exist?
  return if @image_path.nil? || @image_path.to_s == ''

  img = Magick::Image.read(@image_path).first

  p_width = PREVIEW_WIDTH
  p_height = PREVIEW_HEIGHT

  if (img.columns > img.rows) && img.columns > p_width && img.rows > p_height #if it's landscape-oriented
    img.crop!(Magick::EastGravity, img.rows / (p_height / p_width.to_f), img.rows) #Resize it so the right-most part of the image is shown
  end

  img.change_geometry!("#{p_width}>") { |cols, rows, _img| _img.resize!(cols, rows) }

  img.page = Magick::Rectangle.new(img.columns, img.rows, 0, 0)
  img = img.extent(p_width, p_height, 0, 0)
  img.excerpt!(0, 0, p_width, p_height)

  img.write(@thumbnail_path)
rescue Exception => e
  puts "There was an error generating thumbnail: #{e.inspect}"
end