将 PHP GD 中的数组文本定位在图像特定部分的中间

Position array text in PHP GD on middle of specific part of image

我有一个适合页面的字符串数组,我设法将它水平居中,现在我需要在图像的特定部分垂直居中。

     $imgWidth = 240;
    $imgHeight = 900;

    $IMG = imagecreatetruecolor($imgWidth,$imgHeight);
    $font_type_bold = '/dejavu/DejaVuSansCondensed-Bold.ttf';
    $background = imagecolorallocate($IMG, 78,129,154);
    $text_white = imagecolorallocate($IMG, 255,255,255);
    $IMG = imagecreatetruecolor($imgWidth,$imgHeight);
    $max_lenght_of_title = 15;
    $special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame";
    $text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title));
    $line_height=30;
    imageline($IMG, 0, 500, 240, 500, $text_white);
    imageline($IMG, 0, 100, 240, 100, $text_white);
    for($i=0;$i<count($text);$i++) {
        $font_size_si_name_horizontal = 21;
        //Center the text
        $size = imagettfbbox(20, 0, $font_type_bold, $text[$i]);
        $long_text = $size[2] + $size[0];
        $posx = ($imgWidth - $long_text) / 2;

        imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, 150+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);
    }
    header("Content-type: image/png");
    imagepng($IMG);
    imagecolordeallocate($IMG, $text_color );
    imagecolordeallocate($IMG, $background );

结果是这样的,我需要它在页面的特定部分,例如选择一个

那么我怎样才能使它不是固定的中间而是根据文本将在页面的哪一部分进行调整。

注意:文本可以更长,这是主要问题。根据文本的长度,标题应该在中间

<?php
    $imgWidth = 240;
    $imgHeight = 900;
    $IMG = imagecreatetruecolor($imgWidth,$imgHeight);
    $font_type_bold = 'BioRhyme/BioRhyme-Regular.ttf';
    $background = imagecolorallocate($IMG, 78,129,154);
    $text_white = imagecolorallocate($IMG, 255,255,255);

    $IMG = imagecreatetruecolor($imgWidth,$imgHeight);
    $max_lenght_of_title = 15;
    $special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame";
    $text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title));
    $line_height=30;
    $vert = ($imgHeight/5);
    for($i=0;$i<count($text);$i++) {
        $font_size_si_name_horizontal = 21;
        //Center the text
        $size = imagettfbbox(20, 0, $font_type_bold, $text[$i]);
        $long_text = $size[2] + $size[0];
        $posx = ($imgWidth - $long_text) / 2;

        imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, $vert+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);
    }
    header("Content-type: image/png");
    imagepng($IMG);
    imagecolordeallocate($IMG, $text_color );
    imagecolordeallocate($IMG, $background );
?>

我已添加 $vert = ($imgHeight/5); 并放入 imagettftext() 以动态计算垂直 Y 索引!

我找到了解决这个问题的方法

 $middle_of_page = 350;
 for($i=0;$i<count($text);$i++) { 
     $middle_of_page = $middle_of_page - 21;
}
imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5,  $middle_of_page+$line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);

对于 $middle_of_page 部分,我调整了数组大小为 1 的文本所在的点,然后我只删除了代表字符串数组大小的每个 $i 的字体大小 21。