Ruby on rails 3 Rails 3回形针设置背景色-范围,像素为0,0

Ruby on rails 3 Rails 3回形针设置背景色-范围,像素为0,0,ruby-on-rails-3,imagemagick,paperclip,Ruby On Rails 3,Imagemagick,Paperclip,I使用以下选项:转换\u选项 -auto-orient -gravity center -background transparent -extent '50x50>' 对于我的has_attached_file调用,我想做的不是使用透明,而是将图像的背景色设置为图像0,0处的颜色 使用 我得到输出 srgb(48,47,45) 这很好,但我无法在实际通话中用它代替透明 有人能帮我解决这个问题吗?为此编写了一个自定义插值程序 module Paperclip class BgExt

I使用以下选项:转换\u选项

-auto-orient -gravity center -background transparent -extent '50x50>'
对于我的has_attached_file调用,我想做的不是使用透明,而是将图像的背景色设置为图像0,0处的颜色

使用

我得到输出

srgb(48,47,45)
这很好,但我无法在实际通话中用它代替透明


有人能帮我解决这个问题吗?

为此编写了一个自定义插值程序

module Paperclip
  class BgExtrapolator < Processor
    def initialize(file, options = {}, attachment = nil)
      super

      @file             = file
      @instance         = options[:instance]
      @current_format   = File.extname(@file.path)
      @basename         = File.basename(@file.path, @current_format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
      dst.binmode

      begin
        # grab the image
        logo = Magick::Image.read(src.path).first

        # determine the color of the upper left most pixel
        bg_color = logo.pixel_color(0,0)

        # construct an rgba value to provide to imagemagick
        bg_color_value = "rgba(#{bg_color.red >> 8},#{bg_color.green >> 8},#{bg_color.blue >> 8},#{bg_color.to_hsla[3]})"

        parameters = []
        parameters << ":source"
        parameters << "-resize '#{options[:geometry].to_s}' -auto-orient -gravity center -background '#{bg_color_value}' -extent '#{options[:geometry].to_s}'"
        parameters << ":dest"

        # clean the command
        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

        # run the command
        success = convert(parameters, :source => File.expand_path(src.path), :dest => File.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
      rescue Cocaine::CommandNotFoundError => e
        raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
      end

      dst
    end
  end
end
模块回形针
bg类外推器<处理器
def初始化(文件,选项={},附件=nil)
超级的
@file=file
@实例=选项[:实例]
@当前_格式=File.extname(@File.path)
@basename=File.basename(@File.path,@current\u格式)
结束
def制造
src=@file
dst=Tempfile.new([@basename,@format?”。#{@format}”:“”)
dst.binmode
开始
#抓住图像
logo=Magick::Image.read(src.path).first
#确定最左上方像素的颜色
bg_颜色=徽标。像素_颜色(0,0)
#构造要提供给imagemagick的rgba值
bg#u color_value=“rgba({bg#u color.red>>8},{bg#u color.green>>8},{bg#u color.blue>>8},{bg#u color.to_hsla[3]})”
参数=[]
参数e
升起回形针::Error,“处理#{@basename}的缩略图时出错”,如果@whany
解救可卡因::CommandNotFoundError=>e
升起回形针::错误::CommandNotFoundError.new(“无法运行'convert'命令。请安装ImageMagick。”)
结束
dst
结束
结束
结束

为此编写了一个自定义插值程序

module Paperclip
  class BgExtrapolator < Processor
    def initialize(file, options = {}, attachment = nil)
      super

      @file             = file
      @instance         = options[:instance]
      @current_format   = File.extname(@file.path)
      @basename         = File.basename(@file.path, @current_format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
      dst.binmode

      begin
        # grab the image
        logo = Magick::Image.read(src.path).first

        # determine the color of the upper left most pixel
        bg_color = logo.pixel_color(0,0)

        # construct an rgba value to provide to imagemagick
        bg_color_value = "rgba(#{bg_color.red >> 8},#{bg_color.green >> 8},#{bg_color.blue >> 8},#{bg_color.to_hsla[3]})"

        parameters = []
        parameters << ":source"
        parameters << "-resize '#{options[:geometry].to_s}' -auto-orient -gravity center -background '#{bg_color_value}' -extent '#{options[:geometry].to_s}'"
        parameters << ":dest"

        # clean the command
        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

        # run the command
        success = convert(parameters, :source => File.expand_path(src.path), :dest => File.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
      rescue Cocaine::CommandNotFoundError => e
        raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
      end

      dst
    end
  end
end
模块回形针
bg类外推器<处理器
def初始化(文件,选项={},附件=nil)
超级的
@file=file
@实例=选项[:实例]
@当前_格式=File.extname(@File.path)
@basename=File.basename(@File.path,@current\u格式)
结束
def制造
src=@file
dst=Tempfile.new([@basename,@format?”。#{@format}”:“”)
dst.binmode
开始
#抓住图像
logo=Magick::Image.read(src.path).first
#确定最左上方像素的颜色
bg_颜色=徽标。像素_颜色(0,0)
#构造要提供给imagemagick的rgba值
bg#u color_value=“rgba({bg#u color.red>>8},{bg#u color.green>>8},{bg#u color.blue>>8},{bg#u color.to_hsla[3]})”
参数=[]
参数e
升起回形针::Error,“处理#{@basename}的缩略图时出错”,如果@whany
解救可卡因::CommandNotFoundError=>e
升起回形针::错误::CommandNotFoundError.new(“无法运行'convert'命令。请安装ImageMagick。”)
结束
dst
结束
结束
结束

显然,我完全无意中否决了你,界面不允许我撤销它。我把它标出来让版主看看。非常抱歉:/I我显然完全无意中否决了您的投票,并且界面不允许我撤消它。我把它标出来让版主看看。非常抱歉:/