Page 1 of 1

Render text to new image

Posted: 2011-05-17T08:21:34-07:00
by eddieconnecti
Hi there! After search and try for weeks I finally decided to ask you, either if its not possible, to render text (multi- or singleline) into a new image?

So what I really want is a command, to send the following parameters:
- maxwidth
- text
- font to use (like C:\Inetpub\wwwroot\myweb\fonts\arial.ttf)
- fontsize
- fontcolor
- kerning
- wordspacing
- leading
- backgroundcolor ( a color or 'none' )
- path to the file to create (like C:\inetpub\wwwroot\myweb\text1.png)

The fontsize should not be influenced by the image-dimensions, like caption does. Also it would be great to keep the maxwidth optional, so I would have two methods, encapsulating the commands, one for single line text (without maxwidth) and one with multiline text (maximal width by maxwidth). The magick is to find out
- the width and height if the text is singleline
- the height if the text is multiline

All I want is to render the text into a new image so I can resolve the width & height of the image as text bounds after creation and an auto-breaking text if I specify a maxwidth.

Does anyone know if its possible to do this? And if, how...?

Thanks!

Re: Render text to new image

Posted: 2011-05-17T09:12:23-07:00
by Bonzo
I thought you could add a pointsize to caption; anyway try:

Code: Select all

convert -size 230x130 -background lightblue -font verdana.ttf -pointsize 25 -fill black\\
-gravity NorthWest caption:"The quick red fox jumped over the lazy brown dog." \\
-flatten caption1.jpg
If this works try adding your other options.

Re: Render text to new image

Posted: 2011-05-17T10:25:57-07:00
by Bonzo
fmw42 has just pointed out I have made a mistake - posted some very old code and the line continuations are incorrect.

Code: Select all

convert -size 230x130 -background lightblue -font verdana.ttf -pointsize 25 -fill black \
-gravity NorthWest caption:"The quick red fox jumped over the lazy brown dog." \
-flatten caption1.jpg
My mind has gone blank as I normaly use php, is \ correct?

Re: Render text to new image

Posted: 2011-05-17T10:28:54-07:00
by fmw42
I beleive that \ is correct for Linux/Mac (unix) and ^ for Windows as the line continuation symbol

Re: Render text to new image

Posted: 2011-05-17T23:13:25-07:00
by eddieconnecti
Thanks for your help! The code works, it keeps the height dynamic if I use it like that:

Code: Select all

convert -size 400x -background lightblue -fill #ffcc00 -font C:\Inetpub\wwwroot\fonts\verdana.ttf -pointsize 25 -fill black -gravity NorthWest caption:"The quick red fox jumped over the lazy brown dog." -flatten C:\Inetpub\wwwroot\caption.jpg
So I can use it for multiline text. But how can I work with single-line text? In that case I need to get the width...

Re: Render text to new image

Posted: 2011-05-18T18:51:17-07:00
by anthony
for no automatic word wrapping use label: instead of caption. You can include newlines or '\n' in the label for a DIY multi-line label.

http://www.imagemagick.org/Usage/text/

Re: Render text to new image

Posted: 2011-05-19T05:20:59-07:00
by eddieconnecti
Thanks a lot! I think that´s what I need for single line text.

The command I use for single line text is a following:

Code: Select all

 -background #666666 -fill #ffffff -font C:\Inetpub\wwwroot\test\fonts\comicbd.ttf -interword-spacing 10 -kerning 4 -pointsize 10 -gravity West -size x22 label:"Lorem dolor sit amet" -flatten C:\Inetpub\wwwroot\test\201105190217-9675963.png

Re: Render text to new image

Posted: 2011-05-19T17:02:17-07:00
by anthony
That is just arguments, not a command. You can't run "-background" as a command!

Why the -flatten?
Only one image is in memory! Flatten would do nothing in this case.