Search code examples
ruby-on-railsrubyruby-on-rails-4minimagick

How to add background to text using MiniMagick::Image


I'm writing some text on an image with the help of MiniMagick::Image

CODE

image = MiniMagick::Image.open("#{Rails.root}/a.png")

image.combine_options do |c|
  c.fill 'green'
  c.pointsize 20
  c.gravity 'center'
  c.draw "text 0,-110 'Hello I am here'"
  c.draw "text 150,-180 '#{Time.now}"
  c.background 'blue'
end

image.format 'pdf'
image.write("#{Rails.root}/b.pdf")

But, the background doesn't appear

I have referred LINK1 & LINK2


Solution

  • This is how I could get the background property to work:

    image = MiniMagick::Image.open("#{Rails.root}/a.png")
    
    image.combine_options do |c|
      c.gravity 'center'
      c.background 'lightgrey'
      c.splice "0x30"
      c.fill 'green'
      c.pointsize 20
      c.annotate '0', "Hello I am here"
      c.annotate '0', "#{Time.now}"
    end
    
    image.format 'pdf'
    image.write("#{Rails.root}/b.pdf")