4

我正在尝试在图像上写一个字符串。目前我正在使用 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 多行文本和背景图像

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

谢谢大家

4

2 回答 2

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}"
于 2012-11-09T02:12:01.760 回答
0

You need to use Convert with MiniMagick. And caption is great because it will wrap and adjust according to the size, as long as you don't put in pointsize. The syntax is a little tricky though because there aren't many Rails examples out there.

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

Note: You have to add file path last

于 2020-04-14T05:41:21.873 回答