php - 在 gd 和 php 中换行和居中文本

标签 php text image-processing formatting gd

我有一行文本需要在 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())并从它返回的数组计算如何使文本居中。你可以在这里找到它:

center text with imagettfbbox

关于php - 在 gd 和 php 中换行和居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034531/

相关文章:

javascript - 如果按下刷新按钮,则直接转到另一个页面

html - 如何使覆盖文字为 "erased"

c - ANSI C - 计算每行的单词和字母

python - 在图像中绘制隐式函数(Python,[OpenCV])

opencv - 寻找手部图像中的谷点

php - Noobie bigcommerce API 连接

php - 在两个条件下对数组进行排序

php - 如何返回对象数组的响应

java - StringSubstitutor 支持大小写转换

matlab - 如何在 MATLAB 中计算环的半径?