ImageMagick: Why won't this text expand to fil

2019-08-18 06:14发布

问题:

I'm reading the text handling docs for label and it seems if I specify -size it should magically get as big as possible to fill the space.

I want to make it so this text gets as big as possible. I will set the \n characters myself in the title (if absolutely necessary).

I'd rather the \n get calculated automatically, but it seems label is what makes it the biggest possible (but doesn't support automatic \n), while caption will add \n where appropriate (but doesn't support a dynamic size which fills to fit a space).

My goal is to basically get the BIGGEST possible text, whether it's on 1 line or 3, to fit in the green box (between the 2 red ****** lines).

Below are 2 examples of how it won't change. The green color is just temporary so I can debug.

Heres my code for this text piece (without all the rest):

-size 290x54 -background green -fill blue -font ArialB -gravity center label:'Join Us'

Multiline...

-size 290x54 -background green -fill blue -font ArialB -gravity center label:'Join Us Tomorrow\nFor An HVAC Meeting'

I don't know if this is possible, but ideally it should ask "if the title is on 1 line, what is the max size it could be? if I put a \n after word 1, what is the max size it could be? if i put a \n after word 2, what is the max size it could be?"... and then choosing the largest of those.

回答1:

Your command works for me with one correction. Apparently, the pointsize leaks from the your main command into the parenthesis processing, even though I added -respect-parenthesis. So the solution is to add +pointsize into the parenthesis command. Here is my command and result.

magick -respect-parenthesis -size 298x248 canvas:white \( 'arrows.png' -resize 300x \) -gravity northwest -geometry +0+140 -compose over -composite -bordercolor '#cccccc' -border 1 -font Arial -fill red -pointsize 22 -gravity north -annotate +0+4 '*************************' -font Arial -fill red -pointsize 22 -gravity north -annotate +0+68 '*************************' -font Arial -fill green -pointsize 15 -gravity south -annotate +0+64 'hvac.com' \( -size 279x55 +pointsize -background green -fill blue -font ArialB -gravity center caption:'test headline' \) -gravity center -geometry +0-82 -compose over -composite  output.png


Note that I put the arrows.png file where I could access it at the same level as where the command is being issued. You can change the path to it.



回答2:

In ImageMagick use caption by specifying both the widthxheight but not the pointsize. ImageMagick will figure out the proper pointsize to fill the text to the box. For example:

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"Testing" -bordercolor red -border 1 test1.png

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"Should Be Bigger" -bordercolor red -border 1 test2.png

convert -size 500x200 -background white -font arial -fill black -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3.png

You can also include the pointsize, so long as your text is not too long for the area of the box.

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"Testing" -bordercolor red -border 1 test1b.png

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"Should Be Bigger" -bordercolor red -border 1 test2b.png

convert -size 500x200 -background white -font arial -fill black -pointsize 48 -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3b.png

However, if you choose a font that is too big, you will get this:

convert -size 500x200 -background white -font arial -fill black -pointsize 64 -gravity center caption:"This is a title that will be fairly long and I want it to fit nicely" -bordercolor red -border 1 test3c.png