Rails image_tag rotates image

老子叫甜甜 提交于 2019-12-06 06:24:28

问题


I am using Amazon's S3 for image storage with carrierwave and fog configured. The images seem to store correctly however when I have a 'portrait' image (smaller width than height) it is not displaying correctly, but rather rotating the image on its side.

Any pointers in the right direction would be much appreciated!

uploaders/image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  storage :fog

  include CarrierWave::MimeTypes
  process :set_content_type

  process :resize_to_limit => [420, 0]

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg png)
  end

end

show.html.haml

= image_tag(@idea.image_attachments.first.image.url).to_s

image_attachment.rb

class ImageAttachment < ActiveRecord::Base
  require 'carrierwave/orm/activerecord'
  attr_accessible :image, :description
  belongs_to :image_attachable, polymorphic: true
  mount_uploader :image, ImageUploader
end

回答1:


in the uploader.rb file, try

process :auto_orient 

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

it should fix it.




回答2:


Perhaps instead of trying to resize_to_limit, instead create a version that you would like to conform to. For example, if you wanted all pictures to "intelligently" downsize to a square, you could do something like this

remove from uploader:

process :resize_to_limit => [420, 0]

add to uploader:

version :portrait do process :resize_to_fit => [100, 100] end

this way, carrierwave will store the original image, and will then also respond to your call when you do something like this:

@idea.image_url(:portrait)

this is assuming that your Idea model mounts your ImageUploader and has all the other necessary stuff already configured.



来源:https://stackoverflow.com/questions/19165217/rails-image-tag-rotates-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!