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