2016-11-08 61 views
0

我正在使用CarrierWave和MiniMagick處理帳戶個人資料圖片的圖片上傳的網絡應用程序。現在我AvatarUploader類看起來像這樣Ruby on Rails:從移動設備上傳圖片

class AvatarUploader < CarrierWave::Uploader::Base 

    # Include RMagick or MiniMagick support: 
    # include CarrierWave::RMagick 
    include CarrierWave::MiniMagick 

    # In the uploader: 
    def auto_orient 
    manipulate! do |img| 
     img = img.auto_orient 
    end 
    end 

    # Choose what kind of storage to use for this uploader: 
    # storage :file 
    # storage :fog 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    def default_url 
    ActionController::Base.helpers.asset_path('default_avatar.png') 
    end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 

    # Create different versions of your uploaded files: 
    version :thumb do 
    process :auto_orient 
    process :resize_to_fill => [200, 200] 
    end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    # def extension_white_list 
    # %w(jpg jpeg gif png) 
    # end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    # def filename 
    # "something.jpg" if original_filename 
    # end 

end 

,並在表單處理頭像上傳現場看起來像這樣

.row 
    - if f.object.avatar.present? 
    .field 
     = image_tag f.object.avatar.url 
    = f.input :avatar, label: f.object.avatar.present? ? 'Replace Avatar' : 'Avatar' 

這一切都在我的網站的桌面版本的偉大工程,但是當用戶上傳他們從他們的移動電話採取的配置文件圖片它橫向轉向90度,我無法弄清楚原因。我想從this question加入auto_orient代碼將解決這個問題,但它並沒有

回答

0

想通了。

process :auto_orient 

需求version: thumb do

全碼以外的地方叫

# encoding: utf-8 

class AvatarUploader < CarrierWave::Uploader::Base 

    # Include RMagick or MiniMagick support: 
    # include CarrierWave::RMagick 
    include CarrierWave::MiniMagick 

    # In the uploader: 
    def auto_orient 
    manipulate! do |img| 
     img.auto_orient 
     img 
    end 
    end 

    # Choose what kind of storage to use for this uploader: 
    # storage :file 
    # storage :fog 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    def default_url 
    ActionController::Base.helpers.asset_path('default_avatar.png') 
    end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 


    process :resize_to_fit => [400, 400] 
    process :auto_orient 

    # Create different versions of your uploaded files: 
    version :thumb do 
    process :auto_orient 
    process :resize_to_fit => [200, 200] 
    end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    # def extension_white_list 
    # %w(jpg jpeg gif png) 
    # end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    # def filename 
    # "something.jpg" if original_filename 
    # end 

end 
0

信用:@ lando2319
exif image rotation issue using carrierwave and rmagick to upload to s3

試着改變你的auto_orient方法是:

def auto_orient 
    manipulate! do |img| 
    img.tap(&:auto_orient!) #try with and without the ! here. 
    end 
    end 
+0

謝謝您的回答,但遺憾的是它沒有工作。沒有!什麼都沒有發生,並與我有一個錯誤:「阿凡達未能與MiniMagic操縱,也許它不是一個圖像?原始錯誤:'mogrify -auto-orient!/ tmp/mini_magick20161108.jpg'失敗,錯誤:mogrify.im6:無法識別的選項' - 自動定位!' @ error/mogrify.c/MogrifyImageCommand/3918。[我無法看到其餘的,它被切斷] –