使用MiniMagick用标题方法写一个字符串

时间:2012-07-30 22:13:12

标签: ruby imagemagick minimagick

我正在尝试在图像上写一个字符串。目前我正在使用MiniMagick,我可以调整大小并重叠两个图像,但是当我尝试使用标题编写多行字符串时,最终图像没有任何反应,它仍然与之前相同。

这是我目前的代码:

image = MiniMagick::Image.open('template.jpg')
image.combine_options do |c|
  c.background '#0008'
  c.fill '#666'
  c.gravity 'center'
  c.size '100x50'
  c.caption "Lets write some big string here... zzzzz I hope this work =)"
end
image.write('final.jpg')

我的参考: http://www.imagemagick.org/Usage/annotating/

ImageMagick multiline text and background image

http://www.imagemagick.org/www/command-line-options.html#caption

全部谢谢

2 个答案:

答案 0 :(得分:1)

我结束使用系统调用来摆脱这个问题,这是代码:

Subexec.run "convert -background '#fff0' \\
  -fill '#003300' \\
  -gravity west \\
  -size 560x180 \\
  -pointsize 19 \\
  -font \'#{font_path}\' \\
  caption:\"#{caption}\" \\
  #{photo_path} \\
  +swap \\
  -gravity NorthWest \\
  -geometry +333+113 \\
  -composite #{photo_path}"

答案 1 :(得分:0)

您需要将Convert与MiniMagick一起使用。标题很棒,因为只要您不输入pointsize,标题就会根据大小进行包装和调整。语法有点棘手,因为那里没有很多Rails示例。

file = Paperclip::Tempfile.new(["processed", ".jpg"])

MiniMagick::Tool::Convert.new do |img|
  img.background '#0008'
  img.fill '#666'
  img.gravity 'center'
  img.size '100x50'
  img << "caption: Lets write some big string here... zzzzz I hope this work =)"
  img << file.path
 end

model.picture = file
model.save
file.unlink

注意:您必须最后添加文件路径