Apply text to a canvas within a bounding box at max size?

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
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Apply text to a canvas within a bounding box at max size?

Post by owntheweb »

Hello,
I've become quite fond of ImageMagick through PHP's "Imagick" interface/wrapper. However, my shared hosting account where I'll be generating some fun dynamic email headers does not support it. It DOES support direct "convert" command line calls (who would have thought?). Could I trouble someone to help me get to know ImageMagick commands better through this mini-project? :D

Here's my final goal, a header that calls out invitees to an education benefit party by name:
Image

using this fun starting canvas:
Image

and the freely available Comic Book font by Neale Davidson.\

The challenge is sizing the name so that any length will fit:
Image

I could do this with PHP's help if an ImageMagick command could return the text dimensions at a set size, or if there is a "best fit" option that I could use within a restricted bounding box over the base image canvas.

Any tips you could provide would be awesome, and even help me help the Space Foundation make STEM (science technology engineering math) education programs for kids, AWESOME!

Thanks in advance,

Chris

Shameless promotion: You should go and "like" or share this page: http://www.scifisprings.com ;)
Last edited by owntheweb on 2012-10-03T08:27:38-07:00, edited 1 time in total.
Twitter | Blog | Worlds to explore. Worlds to create.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Apply text to a canvas within a bounding box?

Post by Bonzo »

I would use label - http://www.imagemagick.org/Usage/text/#label_bestfit

You can create your lable and as the location will be the same everytime you will know where to place it wit -geometry
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Apply text to a canvas within a bounding box?

Post by owntheweb »

Bonzo,
That may be just the tip I need. I'll respond later this evening with what I piece together if it works.

Thanks!!

Chris
Twitter | Blog | Worlds to explore. Worlds to create.
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Apply text to a canvas within a bounding box at max size

Post by owntheweb »

SUCCESS! Thanks for your feedback Bonzo!

Image

Code: Select all

convert -background transparent -size 187x37 -fill '#022949' -font Comic_Book2.ttf -gravity center label:"ImageMagick," temp.png
composite temp.png -gravity northwest -geometry +158+30 emailBanner1.png temp2.png
Now, is there a way I can do this with one command instead of two?

Thanks again,
Twitter | Blog | Worlds to explore. Worlds to create.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Apply text to a canvas within a bounding box at max size

Post by anthony »

Use the -composite operator..
http://www.imagemagick.org/Usage/compose/#compose

Code: Select all

convert -background transparent -size 187x37 -fill '#022949' -font Comic_Book2.ttf -gravity center \
            label:"ImageMagick," emailBanner1.png  +swap  \
            -gravity northwest -geometry +158+30 -composite  result.png
NOTE the destination image is first in "convert", so I need to swap the order of the images after reading it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Apply text to a canvas within a bounding box at max size

Post by owntheweb »

Hello Anthony,
Thanks for the insightful example. Am I correct in that the image is still being written to the hard drive then read in the second part of your command, or is it being held in memory and only writing result.png? Is there a time/processor savings writing it in a command like this, or does it work about the same way as my example provided?

The technicalities don't matter as much now as my email campaign already went out and worked without any issues. I'm just curious for future fun projects like this.

Best regards,
Twitter | Blog | Worlds to explore. Worlds to create.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply text to a canvas within a bounding box at max size

Post by fmw42 »

While in the same command line, data is kept in memory or memory mapped data until the command finishes. So in your case, there is no write to disk until the command finishes.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Apply text to a canvas within a bounding box at max size

Post by anthony »

Even with multiple commands you can avoid disk writes by using a pipeline to transfer 'written' image data between commands without it actually being written to disk.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Apply text to a canvas within a bounding box at max size

Post by owntheweb »

Cool. I'll look it up. Thanks for your responses!


===
On a side note, I did make an updated email image in a new "Invite a Friend!" feature here:
http://www.scifisprings.com/invite.php

Feel free to try it out. Feedback is welcome. Man, now I have so many ideas to make future stuff even cooler with other non-text elements. Thanks for an awesome tool!
Twitter | Blog | Worlds to explore. Worlds to create.
Post Reply