【发布时间】:2017-02-05 19:11:36
【问题描述】:
我正在使用 Paperclip 转换图像。我注意到如果其色彩空间未设置为 srgb,则生成的图像质量会严重下降。
有没有办法验证上传图片的色彩空间?
【问题讨论】:
标签: ruby-on-rails image paperclip color-space
我正在使用 Paperclip 转换图像。我注意到如果其色彩空间未设置为 srgb,则生成的图像质量会严重下降。
有没有办法验证上传图片的色彩空间?
【问题讨论】:
标签: ruby-on-rails image paperclip color-space
我有一半的答案可能会有所帮助......
您需要安装:https://github.com/rmagick/rmagick
添加到您的模型中:
attr_accessor :image_colorspace
validates_exclusion_of :image_colorspace, in: [Magick::CMYKColorspace], message: 'is not RGB'
before_logo_post_process :read_image_colorspace
def read_image_colorspace
self.image_colorspace = Magick::Image.from_blob(image.queued_for_write[:original].read).first.colorspace
true
end
(将image 替换为您的附件名称。)
...或者,如果你不想使用 rmagick,并且你有一个 'nix 系统,你可以这样做:
attr_accessor :image_colorspace
validates_exclusion_of :image_colorspace, in: ['CMYK'], message: 'is not RGB'
before_logo_post_process :read_image_colorspace
def read_image_colorspace
self.logo_colorspace = `identify -verbose %m "#{image.queued_for_write[:original].path}" | grep 'Colorspace'`.to_s.upcase.strip.split(' ').last
true
end
【讨论】: