问题
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