multiple-lines with image_magick

I'm trying to have a long line of text appear on multiple lines.

Is there a way for the text to wrap automatically?

  <%
  image = MiniMagick::Image.open("public/output4.jpg")
  image.combine_options do |c|
    c.font 'Arial'
    c.pointsize '30'
    c.gravity "center"
    c.size '360x'
    c.fill 'white'
    c.annotate '0,0', "this is a multi-line text paragraph this is a multi-line text paragraph"
  end
  image.write "public/output4.jpg"
    %>

UPDATE I have also tried to replace annotate with caption...and receive the following error

MiniMagick::Error (Command ("mogrify -pointsize 30 -gravity center -size 360x -fill white caption:this is a multi-line text paragraph this is a multi-line text paragraph /var/folders/1s/dh01tgyn3j39f673v1d_xywc0000gn/T/mini_magick20140103-15012-11w3xtz.jpg") failed: {:status_code=>1, :output=>"mogrify: no encode delegate for this image format `this is a multi-line text paragraph this is a multi-line text paragraph' @ error/constitute.c/WriteImage/1195.\n"}):

Answers


You should be able to use a paragraph enabled caption for this, which will let you embed newlines where you want the breaks.

Full documentation on the caption directive and parameters are here: http://www.imagemagick.org/Usage/text/#caption_paragraphs

EDIT: code example

The problem is that your code is using the mogrify utility (by default) and not the convert utility:

  <%
  image = MiniMagick::Image.open("public/output4.jpg")
  image.combine_options('convert') do |c|
    c.font 'Arial'
    c.pointsize '30'
    c.gravity "center"
    c.size '360x'
    c.fill 'white'
    c.annotate '0,0', "this is a multi-line text paragraph\n this is a multi-line text paragraph"
  end
  image.write "public/output4.jpg"
    %>

You could break up the annotate text into separate annotate lines, although that's rather kludgy and likely not what you're looking for.


Caption is the option you are looking for. http://www.imagemagick.org/Usage/text/#caption

MiniMagick seems to have a bug with captions with mogrify https://github.com/minimagick/minimagick/issues/191

What you could do to go around this issue for now is to use the add command method on the builder.

image.combine_options do |c|
  c.add_command "caption", "a very long caption" 
end

What you should do to nail this one down is to first find out the exact mogrify command that you want to apply on the image by referring to the imagemagick documentation and then verify that minimagick is building the same command.

You can see the command built by:

image.combine_options do |c|
  c.add_command "caption", "a very long caption" 
  p c.command  # this should give out the command built
end

Need Your Help

Using generics to enforce valid structure at compile time

c# generics types

By abusing the type system in c#, I can create code, where the compiler will enforce a rule, to ensure an impossible operation is not performed. In the below code, its specific to matrix multiplica...

Cannot read property 'prototype' of undefined; CoffeeScript inheritance with Require.js

javascript coffeescript requirejs

I'm using Require.js and CoffeeScript. I have what I find to be a rather strange error.