【问题标题】:Validate Image Colorspace With Paperclip使用回形针验证图像色彩空间
【发布时间】:2017-02-05 19:11:36
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails image paperclip color-space


    【解决方案1】:

    我有一半的答案可能会有所帮助......

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

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多