在php中动态创建一个垂直按钮


Create dynamically a vertical button in php

我试图在PHP中创建一个垂直按钮,但是我有定位问题,当我创建一个200px高度和40px宽度的PNG时,我想把文本放在该图像的中心。文本长度将动态变化

这是我的代码的一部分

$image = imagecreate($this->width , $this->height);
$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
if ($this->transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);
// render the image
ImageTTFText($image, $this->size, $this->rot, $offset_x, $offset_y, $foreground, $this->font, $this->msg); 

如何计算$offset_x$offset_y

您可以在imagettftext函数的文档中看到,它

返回一个包含8个元素的数组代表四个点组成文本的边界框。的顺序点在左下方,右下方,右上方,左上方。的点是相对于文本而不管的吗角度,所以"左上角"的意思是在你看左上角横向显示文本。返回FALSE错误。

所以你可以先在另一张图片上绘制文本,看看尺寸是多少,然后计算你的按钮所需的高度