Skip to content

Instantly share code, notes, and snippets.

@robertjwhitney
Created March 24, 2012 18:59
Show Gist options
  • Save robertjwhitney/2186736 to your computer and use it in GitHub Desktop.
Save robertjwhitney/2186736 to your computer and use it in GitHub Desktop.
How to access the dimensions of an image in the view using carrierwave and mini_magick
class Photo < ActiveRecord::Base
mount_uploader :file, StillImageUploader
def dimensions(version = nil)
image ||= MiniMagick::Image.open(file.url(version))
{width: image['width'], height: image['height']}
end
end
# photo.dimensions
# => {:width=>545, :height=>300}
#
# photo.dimensions(:thumbnail)
# => {:width=>100, :height=>100}
#
# photo.dimensions[:width]
# => 545
#
# photo.dimensions[:height]
# => 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment