1

I'm working on a web application that uses CarrierWave and MiniMagick to handle image uploads for account profile pictures. Right now my AvatarUploader class looks like this

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

and the field for handling avatar upload in the form looks like this

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

This all works great on the desktop version of my site but when a user uploads a profile pic they take from their mobile phone it gets turned sideways 90 degrees and I can't figure out why. I thought adding the auto_orient code from this question would fix the problem but it did not

Community
  • 1
  • 1
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90

2 Answers2

1

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

try changing your auto_orient method to this:

  def auto_orient
    manipulate! do |img|
    img.tap(&:auto_orient!)  #try with and without the ! here.
    end
  end
Community
  • 1
  • 1
borbesaur
  • 681
  • 4
  • 10
  • Thanks for your answer but unfortunately it did not work. Without the ! nothing happened and with it I got an error: "Avatar failed to manipulate with MiniMagic, maybe it is not an image? Original Error: `mogrify -auto-orient! /tmp/mini_magick20161108.jpg` failed with error: mogrify.im6: unrecognized option `- auto-orient!' @ error/mogrify.c/MogrifyImageCommand/3918. [I cant see the rest, it cut off] – Nick Gilbert Nov 08 '16 at 21:08
0

Figured it out.

process :auto_orient

Needs to be called outside the version: thumb do loop

Full code

# 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
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90