image icon indicating copy to clipboard operation
image copied to clipboard

Wrong text position

Open mihnsen opened this issue 4 years ago • 1 comments

I'm building an application base on the Intervention image - imagick, this library working well so far. But I have a problem with render text with custom fonts. It seems like it is wrong line-height (I know intervention doesn't have line-height at all). Here is the result

  • The top left item was rendered by Monsreat (regular) fonts.
  • The other text was used Parisienne (regular) fonts. image

I deeply research to source code of Intervention (Imagick) and found that line image

Can someone explain why we have to put: 0.65 / 2 to the center - line 67?

$posy = $posy + $dimensions['textHeight'] * 0.65 / 2;

And do you have any idea how can I correct the position for my text? Thanks.

mihnsen avatar Feb 03 '21 17:02 mihnsen

Hi @mihnsen,

I was about the raise an issue asking the same question. In my case, I was planning to create an image and inserting it into the main image. I believe it might fix the issue.

Something like this:

$imagickImage = new \Imagick();
$draw = new \ImagickDraw();
$draw->setGravity(\Imagick::GRAVITY_NORTH);
$draw->setFont($fontSource);

$draw->setFontSize($fontSize);
$draw->setFillColor( 'black');

$info = $imagickImage->queryFontMetrics($draw, 'text');
$imagickImage->newImage((int) $info['textWidth'], (int) $info['textHeight'], 'none');
$imagickImage->annotateImage($draw, 0, 0, 0, 'text');
$imagickImage->setImageFormat('png');

$interventionImage->insert($imagickImage, ...);

jvahldick avatar Feb 10 '21 14:02 jvahldick

I suggest to try this task with Intervention Image 3 which supports line height.

olivervogel avatar Dec 09 '23 15:12 olivervogel