Write a string with caption method using MiniMagick
-
23-06-2021 - |
Question
I'm trying to write a string over a image. Currently I'm using MiniMagick and I can resize and overlap two images, but when I try write a multiline string using caption nothing happens to final image, it's still same as before.
Here is my current code:
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')
My refs: http://www.imagemagick.org/Usage/annotating/
ImageMagick multiline text and background image
http://www.imagemagick.org/www/command-line-options.html#caption
Thanks all
Solution
I ended using a system call to get rid this problem, here is the code:
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}"
OTHER TIPS
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