Imagemagick路轨图象构成增加不需要的白色背景

时间:2017-07-18 18:16:31

标签: 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 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。问题是第二张图像的高度小于第一张图像的高度。图像magick用不需要的白色背景填充剩余部分。我想保持组合图像的透明度。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

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

如果我没弄错的话,这是RMagick,名称可能略有不同(虽然我认为组合选项使用的是mongrifies方法?)。