问题描述
我正在尝试围绕annotateImage
创建包装函数,以便能够设置给定文本的确切顶部和左侧位置.默认方法从基线开始设置y位置,这意味着如果要在图像的确切位置绘制文本,则必须进行大量实验.这就是我的意思...
I'm trying to create a wrapper function around annotateImage
to be able to set the exact top and left positions of a given text. The default method sets the y-position from the baseline, which means there has to be a lot of experimentation involved if one wants to draw a text at an exact spot on an image. This is what I mean...
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');
在上面的代码中,由于y位置为0,因此文本是不可见的.为解决此问题,我从以下函数开始,在该函数中向y添加了40的偏移量.
In the above code, the text is invisible because the y position is 0. So to fix this, I've started with the following function where I add an offset of 40 to y...
function addText($image, $draw, $x, $y, $text) {
$y = $y + 40;
$image->annotateImage($draw, $x, $y, 0, $text);
}
addText($image, $draw, 0, 0, 'The quick brown fox'); // draw at 0, 0
但是它不是很可靠,因为它没有考虑字体大小等因素.
But it's not very reliable because it doesn't take into account any factors such as font size, etc...
实现此目标的最佳方法是什么?
What's the best way to achieve this?
推荐答案
我找到了一个很好的解决方案.如果重力设置为西北",则文本倾向于保持其x y位置从左上方开始.
I've found a solution that works well. If the gravity is set to 'NorthWest', the text tends to keep its x y position from the top left.
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');
这篇关于imagick annotateImage:如何从左上角设置文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!