简体   繁体   English

Rails image_tag旋转图像

[英]Rails image_tag rotates image

I am using Amazon's S3 for image storage with carrierwave and fog configured. 我正在使用亚马逊的S3进行图像存储,并配置了载波和雾。 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 上传/ 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 show.html.haml

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

image_attachment.rb 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

in the uploader.rb file, try 在uploader.rb文件中,试试

process :auto_orient 

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

it should fix it. 它应该解决它。

Perhaps instead of trying to resize_to_limit, instead create a version that you would like to conform to. 也许不是尝试resize_to_limit而是创建一个您想要符合的版本。 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 version:portrait do process:resize_to_fit => [100,100]结束

this way, carrierwave will store the original image, and will then also respond to your call when you do something like this: 这样,carrierwave将存储原始图像,然后当您执行以下操作时也会响应您的呼叫:

@idea.image_url(:portrait)

this is assuming that your Idea model mounts your ImageUploader and has all the other necessary stuff already configured. 这假设你的Idea模型安装了你的ImageUploader并且已经配置了所有其他必要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM