bundle install
:gem 'rails_admin'
# Because rails_admin_jcrop autoload modules by checking plugins you use, it's
# recommended to require it explictly before rails_admin_jcrop
# e.g. if you use carrierwave
# gem 'carrierwave', :require => 'carrierwave'
gem 'rails_admin_jcrop' #, git: 'git://github.com/janx/rails_admin_jcrop.git'
# RAILS_ROOT/config/initializers/rails_admin.rb
config.model User do
configure :avatar, :jcrop
# Below is optional
edit do
field :avatar do
jcrop_options aspectRatio: 500.0/320.0
end
end
end
class AvatarUploader < CarrierWave::Uploader::Base
version :thumb do
process :rails_admin_crop
process resize_to_fill: [500,320]
end
end
rails_admin_jcrop >= 1.0.0
works with rails_admin ~> 0.3.x
. For older rails_admin, use rails_admin_jcrop 0.2.1.You can pass any Jcrop plugin allowed options here, for example, use aspectRatio
to fix the ratio of selection box:
field :avatar do
jcrop_options aspectRatio: 500.0/320.0
end
Please check Jcrop document for more available options.
By default, image is scaled properly to make cropping more easy, but sometimes the image is still too large to fit in the modal window, you may need to scroll image up/down to crop. If you set fit_image
to true, image will always be resized to fit in modal window.
field :avatar do
fit_image true
end
Check screenshots below to see the difference:
When fit_image
is false (default)
When fit_image
is true
Localize the crop form by adding these entries:
zh:
admin:
actions:
crop:
title: '剪裁'
menu: '剪裁'
Any help is encouraged. Here are some ways you can contribute:
Life is easier with you.