如何将文本居中放置在 2 点 php 之间?

How to center text between 2 points php?

我正在尝试将一些文本置于点之间的中心位置。文本是动态的,无论包含什么内容都应居中。

我正在尝试以下操作:

    $dims = imagettfbbox(130, 0, './Calibri.ttf', "JULY");

$width = ($dims[2])-($dims[0]);

imagettftext($my_img , 130, 0, 1196-($width/2) , 700, $text_colour, './Calibri.ttf', "JULY");

我在 x = 711 处有一个点,在 x = 1907 处有一个点,这是我得到 1196 的地方,因为 1907-711 = 1196。

按照我的逻辑,1196-($width/2) 应该起作用,因为参数是文本的第一个像素。

但是从图片上可以看出,它没有居中。

根据您的解释,我了解到 1196-($width/2) 是错误的。

你应该找到盒子的中间,位于 x = 711x = 1907 之间:

$x_center = 711 + (1907 - 711) / 2 
$x_start = $x_center - ($width / 2)

希望对您有所帮助。