我正在尝试在图像上打印多行文本并居中对齐它们.
即
这是一串文字
现在,我只有整个字符串的左侧位置.什么快捷方式让它工作?我认为它可能必须是整个字符串上的getttfbox,然后线上的爆炸中断,然后将新文本集中在更大的ttfbox中.这是屁股的痛苦……
编辑:想出一个解决方案:
foreach ( $strings as $index => $string ) {
$parts = explode ( "\n", $string['string'] );
if ( count ( $parts ) > 1 ) {
$bounds = imagettfbbox ( intval($string['fontsize']), 0, $font, $string['string'] );
$width = $bounds[2] - $bounds[0];
$height = $bounds[3] - $bounds[5];
$line_height = $height / count ( $parts );
foreach ( $parts as $index => $part ) {
$bounds = imagettfbbox ( intval($string['fontsize']), 0, $font, $part );
$new_width = $bounds[2] - $bounds[0];
$diff = ( $width - $new_width ) / 2;
$new_left = $string['left'] + $diff;
$new_string = $string;
$new_string['left'] = $new_left;
$new_string['top'] = $string['top'] + ($index * $line_height);
$new_string['string'] = $part;
$new_strings[] = $new_string;
}
}
}
if ( $new_strings )
$strings = $new_strings;
在这种情况下,每个$string都是一个数组,其中包含有关打印方式和内容的一些信息.
希望能帮助别人.