问题描述
在Rails 5.2上,我试图通过ActiveStorage保存一个化身,但似乎未在活动存储Blob中保存图像整理数据.
On Rails 5.2 I am trying to save an avatar via ActiveStorage but it seems as though not image oriantation data is being saved in the active storage blob.
我正在通过创建动作我的file_field保存化身
I am saving the avatar via a file_field on a create action my
#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 但还没有弄清楚我该如何关联
I have been reading some documentation for minimagick https://github.com/minimagick/minimagick but have not figured out how I can associate
user.avatar.blob
与
image = MiniMagick::Image.open("input.jpg")
我尝试过
image = MiniMagick::Image.open("user.avatar.blob")
但是没有运气
我需要尝试解决这个问题,因为一些存储在活动存储中的化身会旋转90度显示.
I need to try and figure this out because some avatars stored in active storage are being displayed rotated 90 degrees.
https://edgeguides.rubyonrails.org/active_storage_overview.html 关于图像处理的话题,但我对Gems Rails的建议也没有好运
https://edgeguides.rubyonrails.org/active_storage_overview.htmltalks of image processing but I have also had no luck with the gem rails recommends
推荐答案
我认为您要在显示图像时使用一种变体,而不是尝试编辑存储的图像.要确定方向,您可以说:
I think you want to use a variant when displaying the image rather than trying to edit the stored image. To fix the orientation, you could say:
user.avatar.variant(auto_orient: true)
如果您想一次执行多个操作(而不是在管道中),请使用combine_options
:
And if you want to do several operations at once (rather than in a pipeline), use combine_options
:
user.avatar.variant(combine_options: {
auto_orient: true,
gravity: 'center',
resize: '23x42', # Using real dimensions of course.
crop: '23x42+0+0'
})
已编辑的图像将被缓存,因此您仅在首次访问时执行转换工作.您可能希望将variant
放到视图助手中(或者根据您的需要甚至考虑模型问题),以便可以隔离噪音.
The edited image will be cached so you only do the transformation work on first access. You might want to put your variant
s into view helpers (or maybe even a model concern depending on your needs) so that you can isolate the noise.
您可能希望参考API文档以及指南:
You might want to refer to the API docs as well as the guide:
- ActiveStorage::Variant
- ActiveStorage::Variation
这篇关于Rails 5.2 ActiveStorage保存然后读取Exif数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!