0

I am using carrierwave as gem for uploading images in my Rails project, it works fine on all devices except Android, whenever I upload an image from an Android device then it gets rotated 90 degree.

These are the gems used to upload image:

gem 'rmagick'
gem 'carrierwave'

And here is the code from the 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
halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88

1 Answers1

1

Finally i got the solution here https://stackoverflow.com/a/18541893,

here are changes i done to get it fixed:

added this in avatar_uploader.rb:

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

and used it as:

  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

That's it :)

Community
  • 1
  • 1
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88