All pastes #387192 Raw Edit

Something

public text v1 · immutable
·published 2007-03-09 04:21 UTC
rendered paste body
require 'RMagick'

class Image < Upload
  after_create  :create_thumbnail
  
  def initialize(params)
    super
    
    @file.rewind
    @image = Magick::Image.from_blob(@file.read).first
  end
  
  def create_thumbnail
    dimension = post.parent_id ? 175 : 250
    @image.change_geometry("#{dimension}x#{dimension}") { |cols, rows, image|
      if image.columns > dimension or image.rows > dimension then image.resize!(cols, rows, Magick::BoxFilter) end }
    
    FileUtils.mkdir_p(File.dirname(thumbnail_path))  unless File.exist?(File.dirname(thumbnail_path))
    @image.write(thumbnail_path)
  end
  
  def delete_files
    super
    File.delete(thumbnail_path) rescue nil
  end
  
  def get_file_info
    super
    
    self[:width]  = @image.columns
    self[:height] = @image.rows
  end
  
  def thumbnail_path
    return "#{RAILS_ROOT}/public/uploads/thumbnails/#{self[:server_filename]}"
  end
  
  def validate_on_create
    if @image.columns > 10000 or @image.rows > 10000 then errors.add_to_base('The dimensions of this image are too large.') end
  end
end