使用回形针验证图像色彩空间

Validate Image Colorspace With Paperclip

我正在使用 Paperclip 转换图像。我注意到如果其色彩空间未设置为 srgb,则生成的图像质量会严重下降。

有没有办法验证上传图片的色彩空间?

我得到了一半可能有帮助的答案...

您需要安装: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