title: MiniMagick wrap text on image
body: <p>On Rails 5.2.1, I am attempting to use MiniMagick to write text on an image. The problem is, it overflows beyond the bounds of the image's width for long texts.</p>
  
  <p>I have attempted to use <code>draw</code>, <code>label</code>, <code>annotation</code> and <code>caption</code> methods from <a href="https://www.imagemagick.org/Usage/text/#caption" rel="nofollow noreferrer">here</a> but none give me the right result. <code>caption</code> does not even add the text to the image.</p>
  
  <p>This is my code:</p>
  
  <pre><code>      temp_img = MiniMagick::Image.open(url_for(@post.image))
        img_width = temp_img[:width]
  
        temp_img.combine_options do |c|
          c.gravity 'North'
          c.draw "text 0, 0 '#{top_txt}'"
          #c.annotate '0,0', "'#{top_txt}'" (same result)
          #c.caption "'#{top_txt}'" (same result)
          #c.label "'#{top_txt}'" (same result)
          c.gravity 'South'
          c.draw "text 0, 0 '#{bot_txt}'"
          #c.annotate '0,0', "'#{bot_txt}'" (same result)
          #c.caption "'#{bot_txt}'" (same result)
          #c.label "'#{bot_txt}'" (same result)
          c.stroke('#000000')
          c.strokewidth 1
          c.fill('#FFFFFF')
          c.size "#{img_width}x"
          c.pointsize '40'
          c.font "#{Rails.root.join('public', 'font',
          'Franklin_Gothic_Heavy_Regular.ttf')}"
        end
  </code></pre>
  
  <p>This is my result:
  <a href="https://i.stack.imgur.com/kLFH0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kLFH0.png" alt="enter image description here"></a>
  The closest I have seen to a solution is <a href="https://stackoverflow.com/a/26182303/3491653">this</a> but is too messy. </p>
  
  <p>Could there be a better way?</p>