【发布时间】:2026-01-03 22:50:02
【问题描述】:
我的目标是画一个水平居中的m。因此,我计算了字母的宽度,从总宽度中减去该值,最后除以 2。结果应该是与左侧的距离(或与右侧的距离相等)。
但是,“m”总是放错了位置。我还注意到某些字体可能不会触发有问题的行为。请注意,我的脚本适用于所有其他拉丁字符。
宋体:
比特流 Vera Sans:
<?php
$totalWidth = 100;
$totalHeight = 100;
$font = 'Arial.ttf';
$img = imagecreatetruecolor($totalWidth, $totalHeight);
$red = imagecolorallocate($img, 255, 0, 0);
$fontSize = 100;
$bbox = imagettfbbox($fontSize, 0, $font, 'm');
$width = max($bbox[2], $bbox[4]) - max($bbox[0], $bbox[6]);
$centeredX = ($totalWidth - $width) / 2;
imagettftext($img, 100, 0, $centeredX, 100, $red, $font, 'm');
imagepng($img, 'testcase.png');
imagedestroy($img);
【问题讨论】: