Last Updated: February 25, 2016
·
2.586K
· thehappycoder

Prevent automatic rotation of uploaded pictures

Ipad and iphone safari browser tries to be smart and rotates the images according to their exif:Orientation property value. In general cases we don't want that.

Picture

The easiest way is to use jhead command line utility:

$ jhead -norot <path to file>

From jhead manual:
ruby -norot Zero out the rotation tag. This to avoid some browsers from rotating the image again after you rotated it but neglected to clear the rotation tag

If you use ruby and paperclip gem, here is the ready solution:

module Paperclip
  class NoRotation < Processor
    def make
      command = "jhead"
      params = %W[-norot #{abs_path_to_file(@file)}]

      begin
        Paperclip.run(command, params.join(' '))
      rescue ArgumentError, Cocaine::CommandLineError
        raise Paperclip::Error, "There was an error removing rotation for #{@basename}"
      end

      @file
    end

    def abs_path_to_file(destination)
      File.expand_path(destination.path)
    end
  end
end

And in the model apply the processor:

has_attached_file :picture, processors: [:no_rotation]