ruby-on-rails - Rails 5.2 ActiveStorage 保存然后读取 Exif 数据
问题描述
在 Rails 5.2 上,我试图通过 ActiveStorage 保存头像,但似乎没有图像方向数据保存在活动存储 blob 中。
我通过创建操作上的 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
解决方案
我认为您想在显示图像时使用变体,而不是尝试编辑存储的图像。要固定方向,您可以说:
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'
})
编辑后的图像将被缓存,因此您只需在首次访问时进行转换工作。您可能希望将您variant
的 s 放入视图助手(或者根据您的需要甚至可能是模型问题),以便您可以隔离噪音。
您可能需要参考 API 文档和指南:
推荐阅读
- pandas - 为散景中的散点图选择数据框行
- perl - 如何在perl中打印迭代字母
- php - CI 查询结果给出意外结果
- react-native - 使用 react-navigation 导航会导致组件不显示数据,需要重新渲染
- javascript - 更改按钮切换的颜色
- html - 如何创建一个复杂的 3 列 4 行 HTML 表格?
- .net-core - 将控制器的标头信息添加到 httpContextAccessor
- android - 无法解决:firebase-database-15.0.0
- wordpress - Wordpress 博客页面的搜索过滤器
- ruby-on-rails - 如何用 puma 正确处理 Rails 中的并发?