Hello? Any paper_clip wizards out there know if you can when using paperclip to save an image, also save the image dimensions (width&height) in 2 extra fields? How do you get such data during the paperclip upload process?
Thank you
Hello? Any paper_clip wizards out there know if you can when using paperclip to save an image, also save the image dimensions (width&height) in 2 extra fields? How do you get such data during the paperclip upload process?
Thank you
I think that such functionality does not exist in paperclip. If you want to add it, you'll probably want to modify the 'assign' method in lib/paperclip/attachment.rb, where you can get the geometry using Paperclip::Geometry.from_file
You will need to require 'RMagick'
uploaded_image = Magick::Image.read(image).first #image is what you've specified in paperclip to be your image
width = uploaded_image.columns
height = uploaded_image.rows
Not sure how to have it working with the callbacks, though. Maybe something like:
attr_accessor :write_image_dimensions?
before_save :check_image_changed
def check_image_changed
self.write_image_dimensions? = image_changed?
end
after_save :write_image_dimensions, :if => :write_image_dimensions?
def write_image_dimensions
uploaded_image = Magick::Image.read(image).first #image is what you've specified in paperclip to be your image
self.width = uploaded_image.columns
self.height = uploaded_image.rows
save
end