【问题标题】:Rails 5.2 ActiveStorage save and then read Exif dataRails 5.2 ActiveStorage 保存然后读取 Exif 数据
【发布时间】:2019-05-19 03:33:36
【问题描述】:

在 Rails 5.2 上,我试图通过 ActiveStorage 保存头像,但似乎没有图像方向数据保存在活动存储 blob 中。

我正在通过创建操作 my 上的 file_field 保存头像

#user model 

has_one_attached :avatar

private

def avatar_validation
  if avatar.attached?
    if avatar.blob.byte_size > 1000000
      avatar.purge
      errors.add(:avatar, 'file is too large')
    elsif !avatar.blob.content_type.in?(%w[image/png image/jpg 
          image/jpeg])
      avatar.purge
      errors.add(:avatar, 'file type needs to be JPEG, JPG, or PNG')
    end
  end
end

我一直在阅读 minimagick https://github.com/minimagick/minimagick 的一些文档,但还没有弄清楚如何关联

user.avatar.blob 

image = MiniMagick::Image.open("input.jpg")

我试过了

image = MiniMagick::Image.open("user.avatar.blob")

但运气不好

我需要尝试解决这个问题,因为存储在活动存储中的一些头像被旋转 90 度显示。

https://edgeguides.rubyonrails.org/active_storage_overview.html 谈到图像处理,但我对 gem rails 推荐的东西也没有运气

【问题讨论】:

    标签: ruby-on-rails ruby image-processing rails-activestorage minimagick


    【解决方案1】:

    我认为您想在显示图像时使用变体,而不是尝试编辑存储的图像。要固定方向,您可以说:

    user.avatar.variant(auto_orient: true)
    

    如果您想一次执行多个操作(而不是在管道中),请使用combine_options

    user.avatar.variant(combine_options: {
      auto_orient: true,
      gravity:     'center',
      resize:      '23x42',    # Using real dimensions of course.
      crop:        '23x42+0+0'
    })
    

    编辑后的图像将被缓存,因此您只需在首次访问时进行转换工作。您可能希望将您的variants 放入视图助手(或者甚至可能是模型问题,具体取决于您的需要),以便隔离噪音。

    您可能需要参考 API 文档和指南:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-19
      • 2018-10-20
      • 2018-06-24
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多