【发布时间】:2019-01-14 20:12:33
【问题描述】:
在 Rails 5.2.1 上,我正在尝试使用 MiniMagick 在图像上写入文本。问题是,对于长文本,它会超出图像的宽度范围。
我曾尝试使用来自here 的draw、label、annotation 和caption 方法,但没有一个能给我正确的结果。 caption 甚至没有将文本添加到图像中。
这是我的代码:
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
这是我的结果: 我见过的最接近解决方案的是this,但太乱了。
有没有更好的办法?
【问题讨论】:
-
我最终使用了this 答案,因为它给了我最佳/期望的行为
标签: ruby-on-rails ruby image imagemagick minimagick