Skip to content

Instantly share code, notes, and snippets.

@cristibalan
Forked from felipeelias/paperclip_initializer.rb
Created September 18, 2008 18:35
Show Gist options
  • Save cristibalan/11459 to your computer and use it in GitHub Desktop.
Save cristibalan/11459 to your computer and use it in GitHub Desktop.
# Para salvar a altura e largura das imagens enviadas pelo plugin paperclip, utilize o callback abaixo
#
# class Document < ActiveRecord::Base
#
# has_attached_file :document, :styles => { :medium => "300x300>" }
#
# before_save :save_dimensions
#
# def save_dimensions
# if document.image?
# self.width = document.width
# self.height = document.height
# end
# end
#
# end
module Paperclip
class Attachment
def get_and_cache_dimensions(style = default_style)
@_dimensions ||= {}
@_dimensions[style] ||= Paperclip::Geometry.from_file(to_file(style))
end
def width(style = default_style)
get_and_cache_dimensions(style)
@_dimensions[style].width
end
def height(style = default_style)
get_and_cache_dimensions(style)
@_dimensions[style].height
end
def image?(style = default_style)
to_file(style).image?
end
end
module Upfile
def image?
["image/jpeg", "image/tiff", "image/png", "image/gif", "image/bmp"].include?(content_type)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment