Render text to new image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
eddieconnecti
Posts: 4
Joined: 2011-05-17T08:09:34-07:00
Authentication code: 8675308

Render text to new image

Post 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!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Render text to new image

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Render text to new image

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Render text to new image

Post by fmw42 »

I beleive that \ is correct for Linux/Mac (unix) and ^ for Windows as the line continuation symbol
eddieconnecti
Posts: 4
Joined: 2011-05-17T08:09:34-07:00
Authentication code: 8675308

Re: Render text to new image

Post 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...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Render text to new image

Post 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/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
eddieconnecti
Posts: 4
Joined: 2011-05-17T08:09:34-07:00
Authentication code: 8675308

Re: Render text to new image

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Render text to new image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply