【问题标题】:Rails image_tag rotates imageRails image_tag 旋转图像
【发布时间】:2013-10-03 17:13:43
【问题描述】:

我正在使用 Amazon 的 S3 进行图像存储,并配置了carrierwave 和fog。图像似乎可以正确存储,但是当我有一个“纵向”图像(宽度小于高度)时,它无法正确显示,而是将图像侧向旋转。

任何正确方向的指针将不胜感激!

上传者/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

【问题讨论】:

  • 这似乎是来自 iPhone 并具有与之关联的 exif(方向)数据的图像的问题。
  • 我有这个确切的问题,你找到解决办法了吗?

标签: ruby-on-rails amazon-s3 carrierwave fog


【解决方案1】:

在uploader.rb文件中,试试

process :auto_orient 

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

它应该修复它。

【讨论】:

  • 此解决方案确实为新上传修复了它。我仍在为以前的上传文件寻找解决方案,但我不确定能否找到。
【解决方案2】:

也许不是尝试调整大小,而是创建一个您想要符合的版本。例如,如果您希望所有图片“智能”缩小为正方形,您可以这样做

从上传者中删除:

process :resize_to_limit => [420, 0]

添加到上传者:

版本:肖像做 过程:resize_to_fit => [100, 100] 结束

这样,carrierwave 将存储原始图像,然后在您执行以下操作时也会响应您的呼叫:

@idea.image_url(:portrait)

这是假设您的 Idea 模型安装了您的 ImageUploader 并且已经配置了所有其他必要的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多