我有一行文本需要在 GD 图像中换行并居中。我也在使用 ttf 字体。有什么可以帮我的吗?
我已经设法让一些文本包装起来,但现在我需要让它居中:
function wrap($fontSize, $angle, $fontFace, $string, $width){
$ret = "";
$arr = explode(' ', $string);
foreach ( $arr as $word ){
$teststring = $ret.' '.$word;
$testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
if ( $testbox[2] > $width ){
$ret.=($ret==""?"":"\n").$word;
} else {
$ret.=($ret==""?"":' ').$word;
}
}
return $ret;
}
最佳答案
用户 valentijn de Pagter 在 php net 手册上提供了一个很好的函数来计算框(我看到你确实在使用 imagettfbbox())并从它返回的数组计算如何使文本居中。你可以在这里找到它:
关于php - 在 gd 和 php 中换行和居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034531/