0

I had a regular file upload that I now changed to using Cloudinary.

Upon upload I did the following to prevent orientation glitches when uploading images from a mobile device (See exif image rotation issue using carrierwave and rmagick to upload to s3 for details):

process :rotate
process :store_dimensions

def rotate
  manipulate! do |image|
    image.tap(&:auto_orient)
  end
end

def store_dimensions
  # This does not work with cloudinary #18

  if file && model
    model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
  end
end

Neither rotation nor storing the dimensions work, since I switched to cloudinary.

Now Cloudinary has an official tutuorial that shows how to do this but it simply does not work and other people seem to have the same issue and neither of the provided options worked for me:

Community
  • 1
  • 1
Besi
  • 22,579
  • 24
  • 131
  • 223

1 Answers1

0

I was able to get it working using a variation of the first option:

  after_save :update_dimensions

  def update_dimensions
    if self.image != nil && self.image.metadata.present?
      width = self.image.metadata["width"]
      height = self.image.metadata["height"]

      self.update_column(:width, width)
      self.update_column(:height, height)

    end
  end

Important: since we're inside a after_save callback here it's crucial to use update_column so that we don't trigger another callback and end up in an infinite loop.

Fix for the provided solution:

self.image.present? returned false but self.image != nil returend true.

Besi
  • 22,579
  • 24
  • 131
  • 223