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?
Here's my final goal, a header that calls out invitees to an education benefit party by name:
using this fun starting canvas:
and the freely available Comic Book font by Neale Davidson.\
The challenge is sizing the name so that any length will fit:
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
Apply text to a canvas within a bounding box at max size?
Re: Apply text to a canvas within a bounding box?
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
You can create your lable and as the location will be the same everytime you will know where to place it wit -geometry
- 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?
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
That may be just the tip I need. I'll respond later this evening with what I piece together if it works.
Thanks!!
Chris
- 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
SUCCESS! Thanks for your feedback Bonzo!
Now, is there a way I can do this with one command instead of two?
Thanks again,
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
Thanks again,
- 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
Use the -composite operator..
http://www.imagemagick.org/Usage/compose/#compose
NOTE the destination image is first in "convert", so I need to swap the order of the images after reading it.
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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- 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
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,
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,
- 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
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.
- 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
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/
https://imagemagick.org/Usage/
- 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
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!
===
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!