2012-06-10 37 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