mongoid_paperclip_image_dimension
This is a simple gem to persist image dimensions of originals and thumbnails for mongoid documents. It works for both s3 and filesystem storages. This lib is based on this article: stackoverflow.com/questions/4065295/paperclip-saving-the-images-dimentions-width-height
Installation
You can simply install from rubygems:
gem install mongoid_paperclip_image_dimension
or in Gemfile:
gem 'mongoid_paperclip_image_dimension'
Basic Usage
class Post
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Paperclip::ImageDimension
has_mongoid_attached_file :image, :styles => {
:large => ['350x350>', :jpg],
:medium => ['150x150>', :jpg],
:small => ['30x30>', :jpg]
}
has_mongoid_attached_file :another_image, :styles => {
:large => ['350x350>', :jpg],
:medium => ['150x150>', :jpg],
:small => ['30x30>', :jpg]
}
# This will save all dimensions of images and thumbnails for both :image and :another_image
save_image_dimensions_on :image, :another_image
end
# retrieve image dimensions
p = Post.first
p.image_dimensions # If the original image has width of 2048 and height of 1024, the output should be:
# {
# 'original' => [2048, 1024],
# 'large' => [350, 175],
# 'medium' => [150, 75],
# 'small' => [30, 15]
# }
p.another_image_dimensions # If the original image has width of 2048 and height of 1024, the output should be:
# {
# 'original' => [2048, 1024],
# 'large' => [350, 175],
# 'medium' => [150, 75],
# 'small' => [30, 15]
# }
}
# some sugar:
p.image_dimension # [2048, 1024]
p.image_dimension(:original) # [2048, 1024]
p.image_dimension(:large) # [350, 175]
p.image_dimension_str # "2048x1024"
p.image_dimension_str(:original) # "2048x1024"
p.image_dimension_str(:large) # "350x175"
Contributing to mongoid_paperclip_image_dimension
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2011 Aaron Qian. See LICENSE.txt for further details.