Hi, I am new to the forum but have used the ImageMagick site often to learn and create different php scripts using IM.
I am currently working on a php script to add text on graphics, and I am trying to determine if there is a method to keep the text centered when offsetting the starting location to the east or west.
This is how I add text to the image:
-gravity $loca1 -fill rgb\($red,$green,$blue\) -annotate +$x+$y '$text'
I supply the gravity location, the x & y parameters and text via php variables.
When I choose Center for gravity and then offset x, the text it is no longer centered. Is there a argument or some formula I can apply/include that could take into account this offset while keeping the text centered so no matter the length of the text entered it would still be centered instead of starting x amount of pixels from center of the image?
Can the center coordinates be accessed from ImageMagick? If so, could the offset be added to the center x coordinate to move the text however many pixels to the left or right but keep it centered?
Thanks,
Teresa
How to center text after offsetting
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to center text after offsetting
Not sure I understand! you want them centered or you want them offset. You cannot have both. If you want them centered, use -annotate +0+0 after setting -gravity center. By specifying gravity any offsets will be relative to the gravity setting.
Perhaps I misunderstand. Can you be more specific about what you mean by having it centered after having set some non-zero offsets. This is confusing to me.
Perhaps I misunderstand. Can you be more specific about what you mean by having it centered after having set some non-zero offsets. This is confusing to me.
Re: How to center text after offsetting
Sorry, I confuse myself all the time...
What I am trying to accomplish is to select a starting location for the text using gravity, but then to fine tune it but have it centered at the new starting location,
so if my image was 300x300 and I used Center for gravity, it would center the text at 150x150. my image has a circle at 150x150 and I want the text to be to the right of the circle, centered in the remaining space between the circle and the edge of the image.
Does that make sense? I'm sorry I am not explaining this well.
What I am trying to accomplish is to select a starting location for the text using gravity, but then to fine tune it but have it centered at the new starting location,
so if my image was 300x300 and I used Center for gravity, it would center the text at 150x150. my image has a circle at 150x150 and I want the text to be to the right of the circle, centered in the remaining space between the circle and the edge of the image.
Does that make sense? I'm sorry I am not explaining this well.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: How to center text after offsetting
The problem is that text (and image) "Justification" is currently rolled into the "Gravity" positioning setting. The two are related but should really be completely separate options.
It would be nice if a separate 'justification' option was present that would only default to 'gravity' if the user has not set it. This has been a requested feature for many years now.
As SVG has the concept of "justification", but not "gravity", I am surprised that they are not already separate options.
Of course that means you need to 'DIY' the situation
It would be nice if a separate 'justification' option was present that would only default to 'gravity' if the user has not set it. This has been a requested feature for many years now.
As SVG has the concept of "justification", but not "gravity", I am surprised that they are not already separate options.
Of course that means you need to 'DIY' the situation

Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/