You can easily save height and width as attributes with your model if you use Rmagick. In the Carrierwave bootloader:
class ArtworkUploader < CarrierWave::Uploader::Base def geometry @geometry ||= get_geometry end def get_geometry if @file img = ::Magick::Image::read(@file.file).first geometry = { width: img.columns, height: img.rows } end end end
And in your model:
class Artwork < ActiveRecord::Base mount_uploader :image, ArtworkUploader before_save :save_image_dimensions private def save_image_dimensions if image_changed? self.image_width = image.geometry[:width] self.image_height = image.geometry[:height] end end end
JamieD
source share