2016-09-21 36 views
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 

回答

0

最後我得到了解決此https://stackoverflow.com/a/18541893代碼,

這裏有變化我做了修復:

加了這個在avatar_uploader.rb

def auto_orient 
    manipulate! do |img| 
     img = img.auto_orient 
    end 
end 

,並用它作爲:

version :thumb, :if => :image? do 
    process :auto_orient  // here it is used 
    process :crop 
    resize_to_fill(100, 100) 
    end 

    version :large, :if => :image? do 
    process :auto_orient  // here it is used 
    resize_to_limit(600, 600) 
    end 

就是這樣:)