views:

64

answers:

3

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

A: 

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

Roman
is there any way I could use ImageMagick during the upload process?
TheExit
yes, check the Paperclip::Processor class. Check test/style_test.rb for ideas.
Roman
A: 

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
jordinl