class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
这样有个好处,可以利用 model 传入各种操作参数,如:
123456789101112131415161718
def crop
if model.crop_x.present?
resize_to_limit(700, 700)
manipulate! do |img|
x = model.crop_x
y = model.crop_y
w = model.crop_w
h = model.crop_h
size = w << 'x' << h
offset = '+' << x << '+' << y
img.crop("#{size}#{offset}") # Doesn't return an image...
img # ...so you'll neet to call it yourself
end
end
end
version :thumb do
process :lazy_resize_300
process :cropper
process :force_resize_100x100!
end
def lazy_resize_300
manipulate! do |img|
img.resize "300x300^" if img[:width].to_i < 300 && img[:height].to_i < 300
img.resize "300x300>"
img
end
end
def force_resize_100x100!
manipulate! do |img|
img.resize "100x100!"
img
end
end
def cropper
manipulate! do |img|
x = model.crop_x
y = model.crop_y
w = model.crop_w
h = model.crop_h
size = w << 'x' << h
offset = '+' << x << '+' << y
img.crop("#{size}#{offset}") # Doesn't return an image...
img # ...so you'll neet to call it yourself
end
end