1

I am using PHP ImageMagick (Imagick) for creating images consisting of multiple images and texts, uploaded and added by visitors of my website. It's actually a kind of canvas where a user can upload their images, position them, optionally add some text and after a click on the button, all the images and texts will be bundled to one image.

Everything works fine, but right now I want to add another feature: text-align: center. I want my user to be abled to add text aligned in the center.

For adding normal (left aligned) text is use this code:

$text_temp = new ImagickDraw();
$text_temp->setGravity(Imagick::GRAVITY_NORTHWEST);
...
$image->annotateImage($text_temp, $new_left, $new_top, $angle, html_entity_decode($item->text));

$new_left, $new_top, $angle are calculated in between and are correct. When I left align the text, this are the results: Input and output as they should be

But, here comes the problem. I want the text to be centered. So I changed my code to this:

$text_temp = new ImagickDraw();
if($item->textalign == 'center') {
    $text_temp->setGravity(Imagick::GRAVITY_NORTHWEST);
    $text_temp->setTextAlignment(Imagick::ALIGN_CENTER);
}
...
$image->annotateImage($text_temp, $new_left, $new_top, $angle, html_entity_decode($item->text));

The text is indeed centered, but the position is not correct anymore. See the input and output results: Strange positions when text is aligned in the center

The reason I use setGravity is because I can position the text from the top left. But right now it looks like it's positioning from the center again and I have no idea what I'm doing wrong.

The $new_left and $new_top variables are positive numbers, multiplied by a positive factor, so there is no way the x- or the y-positions are negative.

Does anyone have an idea?

UPDATE: @MarkSetchell Thanks for your reply, it putted me in the right direction. I didn't do it exactly like you, but I fixed the problem by adding a wrapper (span) around the user text. I already saved the width and height of images, so it was easy to do the same with texts. With these information I can determine the center of the text, and thus position it from center. This worked for horizontal positioning:

$text_temp->setTextAlignment(Imagick::ALIGN_CENTER);
$new_left = $new_left + ($new_width / 2);
//$new_top = $new_top + ($new_height / 2);
$new_top = $new_top + ($new_fontsize);

But it didn't for vertical positioning. Still don't know why, but I handle it now as if I position from top (not center). Then add one time the font size of the text. I can't explain why this is working, but it works. Can somebody tell me?

Jos
  • 62
  • 1
  • 7
  • Try setting gravity to `center` before setting the text alignment, then setting it back to northwest before annotating... maybe. – Mark Setchell Oct 04 '16 at 16:46
  • Tried it, but without success. Looks like it's not even doing anything with the gravity when I align the text in the center. – Jos Oct 05 '16 at 06:47
  • You might find it easier to create a canvas with a transparent background and write your text onto it centred. Then trim the canvas to get rid of any unnecessary space around it and then use `extent` to extend the canvas out to the correct size you want, then composite that onto your image. I did that here http://stackoverflow.com/a/39766014/2836621 – Mark Setchell Oct 05 '16 at 08:37
  • Please can you provide a working code example. Also if you think it's a bug, maybe open a ticket at https://github.com/mkoppanen/imagick Fyi - I remain unconvinced that the gravity settings work correctly _at all_. – Danack Oct 05 '16 at 16:08
  • @MarkSetchell thanks for your reply, I updated my post with a workaround based on your idea. – Jos Oct 06 '16 at 11:27
  • @Danack, I'm sorry but I can't, it's a very long and complicated script. – Jos Oct 06 '16 at 11:27

0 Answers0