【问题标题】:How to render text on image with RMagick and Word wrap?如何使用 RMagick 和自动换行在图像上呈现文本?
【发布时间】:2012-08-03 01:54:11
【问题描述】:

我需要在图像 (1024x768) 上渲染一些文本(unicode、helvetica、白色、22px、粗体)。

这是我目前的代码:

img = Magick::ImageList.new("my_bg_img.jpg")
txt = Magick::Draw.new

img.annotate(txt, 800, 600, 0, 0, "my super long text that needs to be auto line breaked and cropped") {
      txt.gravity = Magick::NorthGravity
      txt.pointsize = 22
      txt.fill = "#ffffff"
      txt.font_family = 'helvetica'
      txt.font_weight = Magick::BoldWeight
}

img.format = "jpeg"

return img.to_blob

一切都很好,但它不会自动换行(自动换行)以将所有文本放入我定义的区域(800x600)。

我做错了什么?

感谢您的帮助:)

【问题讨论】:

    标签: ruby rmagick


    【解决方案1】:

    在 Draw.annotate 方法中,width 参数似乎对呈现文本没有影响。

    我遇到了同样的问题,我开发了这个函数,通过添加新行来使文本适应指定的宽度。

    我有一个功能可以检查在图像上绘制时文本是否适合指定的宽度

    def text_fit?(text, width)
      tmp_image = Image.new(width, 500)
      drawing = Draw.new
      drawing.annotate(tmp_image, 0, 0, 0, 0, text) { |txt|
        txt.gravity = Magick::NorthGravity
        txt.pointsize = 22
        txt.fill = "#ffffff"
        txt.font_family = 'helvetica'
        txt.font_weight = Magick::BoldWeight
      }
      metrics = drawing.get_multiline_type_metrics(tmp_image, text)
      (metrics.width < width)
    end
    

    我还有另一个函数可以通过添加新行来转换文本以适应指定的宽度

    def fit_text(text, width)
      separator = ' '
      line = ''
    
      if not text_fit?(text, width) and text.include? separator
        i = 0
        text.split(separator).each do |word|
          if i == 0
            tmp_line = line + word
          else
            tmp_line = line + separator + word
          end
    
          if text_fit?(tmp_line, width)
            unless i == 0
              line += separator
            end
            line += word
          else
            unless i == 0
              line +=  '\n'
            end
            line += word
          end
          i += 1
        end
        text = line
      end
      text
    end
    

    【讨论】:

    • 谢谢!这项工作按预期工作,无需重新实施即可节省大量时间。
    • ? 你是男人中的圣人
    【解决方案2】:

    试试这个:

    phrase = "my super long text that needs to be auto line breaked and cropped"
    background = Magick::Image.read('my_bg_img.jpg').first
    image = Magick::Image.read("caption:#{my_text}") do
      self.size = '800x600' #Text box size
      self.background_color = 'none' #transparent
      self.pointsize = 22 # font size
      self.font = 'Helvetica' #font family
      self.fill = 'gray' #font color
      self.gravity = Magick::CenterGravity #Text orientation
    end.first
    background.composite!(image, Magick::NorthEastGravity, 20, 40, Magick::OverCompositeOp)
    background.format = "jpeg"
    return background.to_blob
    

    【讨论】:

    • 效果惊人!干净得多。请注意,您应该在第三行使用“phrase”,而不是“my_text”。
    【解决方案3】:

    找到一些代码here 可以解决问题:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多