【发布时间】:2019-04-12 09:35:54
【问题描述】:
我已经创建了这样的图像:
$altoRouter->map('GET|POST', '/public/images/generate/[*:name]', function($string) {
$font = 100;
$im = imagecreatetruecolor($font * strlen($string['name']) + 50, 300);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, $font, 0, 0, $font - 3, $black, IEZON_ROOT . "/public/uploads/NovaSquare.ttf", strtoupper($string['name']));
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
});
如果我这样做/public/images/generate/example,这会给我这个输出:
但是,如您所见,文本是左对齐的,而不是在图像的中心。有什么办法可以实现吗?
【问题讨论】:
-
查看php.net/imagettftext 上投票最多的评论(搜索“水平居中示例”)。您需要
imagettfbbox来计算文本的大小并相应地调整位置。 -
谢谢!我会用更新的代码@ceejayoz 来回答它