Ruby on rails Imagemagick rails图像合成添加不需要的白色背景
我有以下代码:Ruby on rails Imagemagick rails图像合成添加不需要的白色背景,ruby-on-rails,ruby,imagick,Ruby On Rails,Ruby,Imagick,我有以下代码: 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 need
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用不需要的白色背景填充剩余部分。我想保持组合图像的透明度。您可以执行以下操作:
manipulate! do |img|
img.combine_options do |c|
c.background "transparent"
c.gravity "center"
c.extent "450x401"
end
end
这是给RMagick的,如果我没有弄错的话,名称可能会略有不同(尽管我认为combine options使用的是mongrifies方法?)。@Kerby82好的