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



我正在使用回形针来转换图像。我注意到,如果生成的图像的色彩空间未设置为 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

最新更新