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: 49
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 » 2017-08-27T14:05:16-07:00

kk fmw, let me try the above code first however could you point to some of those Windows PHP users.
the spacing between the text is causing lots problem. hw can i solve these? any PHP user home??
Last edited by dt59 on 2017-08-27T14:23:08-07:00, edited 1 time in total.

dt59
Posts: 49
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 » 2017-08-27T14:11:47-07:00

pls can somebody explain -trim, +repage and -smush.
and why the signs - and +.

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T15:33:34-07:00

-trim removes constant colors around the image. The +repage is to make sure the output dimensions are the same as the trimmed dimension; that is remove the virtual canvas that holds the original size and crop offset. It is the same as -repage +0+0. The -smush put an offset between the two images vertically. The +smush does the same horizontally. It is like append except it puts space between them.

See http://www.imagemagick.org/script/comma ... ptions.php for each operator and also
http://www.imagemagick.org/script/comma ... .php#smush
http://www.imagemagick.org/Usage/crop/#trim

For novices, see

viewtopic.php?f=1&t=9620
http://http://www.imagemagick.org/scrip ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

dt59
Posts: 49
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 » 2017-08-27T16:31:41-07:00

hello Fred have read some of these stuff here on IM bt don't have a full understanding as the way u just briefly explained it. am sorry for my newbie questions.
please Wat the difference between xc and the background image operator??

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T16:53:15-07:00

xc: is the same as canvas: Both are used to create a color image in combination with -size. They are called pseudoimages, since the are used internally to imagemagick. See http://www.imagemagick.org/script/formats.php#pseudo

dt59
Posts: 49
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 » 2017-08-27T21:14:48-07:00

well explained am beginning to understand hw IM is working. i can now create some IM command on my own, thanks fred for the kind lectures.
finally have fixed the spacing problem by using /"your string here/" :D :D :D

fred is their a way i could set the height and width of the red border so that i can truncate any text beyond the border

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T22:50:31-07:00

dt59 wrote:fred is their a way i could set the height and width of the red border so that i can truncate any text beyond the border
Do you mean the box size or the thickness of the lines?

The way they both are set is from the command:

Code: Select all

-bordercolor white -border 10 -bordercolor red -border 10
The first white border is padding the image to make the box larger. The second red border is setting the color and thickness of the box lines. Change the first 10 to increase/decrease the size of the box. Change the second 10 to make the border thicker/thinner.

Is that what you want?

dt59
Posts: 49
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 » 2017-08-27T22:52:41-07:00

box size, i want to truncate the text if the text entered by the users is longer than box size.
Last edited by dt59 on 2017-08-27T22:58:29-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T22:56:55-07:00

The box size cannot be made smaller than the trimmed composite image (to remove text), unless you crop it specifically after the trim and composite. The way my code works is to create a text image and trim to its bounding box.. Create a second text image and trim to its bounding box. Then smash them with some space to append them. Then add so white border to pad the area around both text. Then add the red border. Perhaps you can explain more what you want and show a diagram or example.

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T22:57:55-07:00

Why do you want to trim off text?

dt59
Posts: 49
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 » 2017-08-27T22:59:35-07:00

i want to truncate the text if the text entered by the users is longer than box size, from what am seeing now, if the user enter a text longer than box the box then extent to the side of the 500x500 background.

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T23:02:30-07:00

If you want a specific box size then you need to use my other code that I showed earlier that uses -annotate and -draw. With -draw, you can specify the box size. But the text can extend outside the box if you do not pick the right font size. So I am not sure this is what you want.

Code: Select all

convert -size 500x500 xc:white \
-fill black -pointsize 36 -font arial -gravity northwest \
-annotate +78+167 "my first text" \
-annotate +85+213 "center text" \
-fill none -stroke red -strokewidth 10 \
-draw "rectangle 65,155 275,263" -alpha off \
text_result2.png
To draw a box of just the right size. You need to create a white background image of that size, use annotate to add your text. If the text is too long, it will be truncated by the background image. Then add your color border around that. You will have to make your background image sized to account for the thickness of the border you add at the end. Then composite that at the desired location in your larger 500x500 background

dt59
Posts: 49
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 » 2017-08-27T23:09:16-07:00

i will want to maintain the center text alignment by using the gravity = center, because i don't known the length of the text the user is going to type in. therefore i may not want to manually tweak the position of the text. however i will want the text to be at the center of the box. is that possible??
hope you understand wat am trying to do, fred?

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T23:14:47-07:00

Unfortunately, that is hard. The caption: option will allow the text to wrap and center. But then it will never exceed the width of the image it creates, but it could exceed the height. The -annotate option allows you to write individual lines of text, but you have to position them so that they center the way you want. Similar with -draw. I do not know of a way to have multiple lines of text and have them cropped by the image and centered at the same time.

You may look further into PANGO: and see if it can do that. I am not an expert on PANGO and it has its own markup language.
See http://www.imagemagick.org/Usage/text/#pango and http://www.pygtk.org/docs/pygtk/pango-m ... guage.html

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
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 » 2017-08-27T23:18:46-07:00

Best I can suggest is to crop the code I gave you first. See the red code and choose your gravity setting and crop WxH.

<?php
$ou = "magick ";
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:^"my first text^" -trim +repage ) ";
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:^"center text^" -trim +repage ) ";
$ou .= "-gravity center -smush +20 -gravity ZZZ -crop WxH +repage -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "( -size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

Post Reply