2017-07-18 77 views
0

我有以下代碼:ImageMagick的導軌圖像組合物中加入不想要的白色背景

begin 

     big_image = Magick::ImageList.new 

     #this is an image containing first row of images 
     first_row = Magick::ImageList.new 




     #adding images to the first row (Image.read returns an Array, this is why .first is needed) 
     first_row.push(Magick::Image.read(Rails.root.join("app","assets","images","logo.png")).first) 

     if @model.avatar.exists? 
     image = Magick::Image.read(@model.avatar.path).first 

     image = image.resize_to_fit("450", "401") 


     first_row.push(image) 

     end 


     #adding first row to big image and specify that we want images in first row to be appended in a single image on the same row - argument false on append does that 
     big_image.push (first_row.append(false)) 


     fileName = @model.id.to_s + ".png" 
     big_image.append(true).write(Rails.root.join("app","assets","images","shared_logo",fileName)) 



    rescue => e 
     puts "Errors! -- #{e.inspect}" 
    end 

的代碼把在同一行上的兩個圖像。圖像是PNG。問題是第二個圖像的高度比第一個低。 Image magick用不需要的白色背景填充剩餘部分。我想保留組合圖像的透明度。

回答

0

你可以這樣做:

manipulate! do |img| 
    img.combine_options do |c| 
    c.background "transparent" 
    c.gravity  "center" 
    c.extent  "450x401" 
    end 
end 

這是RMagick如果我沒有記錯的話,名稱可能會略有不同(雖然我覺得組合選項是使用mongrifies方法?)。

+0

不起作用... – Kerby82

+0

@ Kerby82 ok then –