Sha256: 8806fc24b3fb1e19bb99230333890c20347574c724bf5ffd33bfa2d952c45af3

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

class Mangos::Thumbnailer
  #PREVIEW_WIDTH = 211
  #PREVIEW_HEIGHT = 332
  PREVIEW_WIDTH = 197
  PREVIEW_HEIGHT = 310

  PREVIEW_SMALL_WIDTH = 98
  PREVIEW_SMALL_HEIGHT = 154

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

  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mangos-0.5 lib/mangos/thumbnailer.rb
mangos-0.4 lib/mangos/thumbnailer.rb
mangos-0.3 lib/mangos/thumbnailer.rb
mangos-0.2 lib/mangos/thumbnailer.rb
mangos-0.1 lib/mangos/thumbnailer.rb