Saving the images Dimensions (width and height) in Paperclip?

TheExit picture TheExit · Oct 31, 2010 · Viewed 10.6k times · Source

Any Paperclip wizards out there know if you can when using Paperclip to save an image, also save the image dimensions (width and height) in 2 extra fields? How do you get such data during the Paperclip upload process?

Answer

Nikita Rybak picture Nikita Rybak · Dec 5, 2010

Just for the sake of completeness, even though previous answers already show good enough suggestions.

You can utilize Paperclip event handlers instead of Rails callbacks. In this case, size will be recalculated only when image changes. (If you're using S3 for storage, this can save quite some time)

has_attached_file :image, :styles => ...
after_post_process :save_image_dimensions

def save_image_dimensions
  geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
  self.image_width = geo.width
  self.image_height = geo.height
end

Image don't even have to be downloaded from S3 (or read from a file), paperclip provides it to event handler itself.

See Events section of the readme for details.