代码之家  ›  专栏  ›  技术社区  ›  Nick Gilbert

Ruby on Rails:从移动设备横向上传图像

  •  1
  • Nick Gilbert  · 技术社区  · 9 年前

    我正在开发一个web应用程序,它使用CarrierWave和MiniMagick处理帐户配置文件图片的图像上传。现在,我的AvatarUploader类看起来像这样

    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度,我不知道为什么。我想从中添加auto_orient代码 this question 会解决问题,但没有

    2 回复  |  直到 9 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    信用证:@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
    
        2
  •  0
  •   Nick Gilbert    9 年前

    找到了。

    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