hw to offset text from the top of an image and give it a text alignment of center

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

hello fred, did u test the code. am having undesired result,check pls
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

When in doubt, separate the commands and look at each intermediate result to see what is happening.

Code: Select all

convert -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage tmp1a.png
convert -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage tmp2a.png
convert tmp1a.png tmp2a.png -gravity center -smush +20 -bordercolor white -border 10 tmp3a.png
convert tmp3a.png -bordercolor red -border 10 tmp3b.png
convert -size 500x500 xc:white tmp3b.png -geometry +57+146 -compose over -composite text_resultA.png
convert tmp3a.png -format "%wx%h" info:
202x99
Image

In the above, I found the size of the text image before adding the red border. So I cropped it a bit smaller in width in the following.

Code: Select all

convert -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage tmp1a.png
convert -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage tmp2a.png
convert tmp1a.png tmp2a.png -gravity center -smush +20 -bordercolor white -border 10 tmp3a.png
convert tmp3a.png -gravity northwest -crop 184x99+0+0 +repage -bordercolor red -border 10 tmp3b.png
convert -size 500x500 xc:white tmp3b.png -geometry +57+146 -compose over -composite text_resultB.png
Image


The above is likely not what you want as I mentioned above in my previous email. And as I said before, there is no easy way that I know to do what you want. Imagemagick cannot do both wrap and trim text that is too long.

So what I think you have to do is write the text with -annotate or -draw and place each line where you want on an image of the size you want for your box before adding the border. Then add the border. Then composite image in your 500x500 white background in the place you want. This is what I said in my last email message.

Please provide an example of the way you want your result to look.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

thanks Fred, is now working perfectly well as i expected. Am still learning anyway
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

PHP exec() is no different from command line imagemagick. You just put your command in the exec("....") area.

If you want other API, there is Imagick and Magickwand for PHP. See those forums on this web site.

See also http://www.rubblewebs.co.uk/index.php
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

hello moderator, please is these forum topic still open, i will want to ask some questions base on my previous questions. permitted ?? or i should create a new topic.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

Best to create a new topic after so long a time.

If it is really relevant, then you can link back to this topic in your new post.
Post Reply