Page 1 of 1

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

Posted: 2012-10-03T06:27:29-07:00
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 ;)

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

Posted: 2012-10-03T07:55:13-07:00
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

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

Posted: 2012-10-03T08:05:27-07:00
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

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

Posted: 2012-10-03T19:34:08-07:00
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,

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

Posted: 2012-10-07T18:33:37-07:00
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.

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

Posted: 2012-10-08T06:52:01-07:00
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,

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

Posted: 2012-10-08T09:37:20-07:00
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.

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

Posted: 2012-10-09T22:32:30-07:00
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.

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

Posted: 2012-10-16T12:57:22-07:00
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!