2016-11-08 48 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。[我无法看到其余的,它被切断] –