0
我使用carrierwave
作爲在我的Rails項目中上傳圖片的gem,它在任何設備上工作都很好,只要我從Android設備上傳圖片,然後旋轉90度即可。圖片在Rails上傳時自動旋轉
這些寶石用來上傳圖片:
gem 'rmagick'
gem 'carrierwave'
這裏是從avatar_uploader.rb
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb, :if => :image? do
process :crop
resize_to_fill(100, 100)
end
version :large, :if => :image? do
resize_to_limit(600, 600)
end
def crop
if model.crop_x.present?
resize_to_limit(600, 600)
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
img.crop!(x, y, w, h)
end
end
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
end