2012-06-10 38 views
0

设置与像素-extent的背景颜色我使用下列内容:convert_optionsRails 3的回形针在0,0

-auto-orient -gravity center -background transparent -extent '50x50>' 

为我has_attached_file电话,我想要做的是什么,而不是使用透明的,我想要将图像的背景颜色设置为图像0,0处的颜色。

使用

convert rose: -format "%[pixel:u.p{0,0}]" info:- 

我得到的输出

srgb(48,47,45) 

这是伟大的,但我不能使用,在地方的实际通话透明..

灿任何人都可以帮我解决这个问题?

回答

3

为此写了一个自定义插补器。

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 
+0

我明显低估了你的意外,并且界面不允许我撤消它。我将其标记为供版主查看。非常抱歉:/ – spike

+0

Nvm让我在编辑后更改我的投票。 – spike