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

将PHP GD中的数组文本放置在图像特定部分的中间,php,gd,Php,Gd,我有一个字符串数组可以放在页面上,我设法使它水平居中,现在我需要在图像的特定部分垂直居中 $imgWidth = 240; $imgHeight = 900; $IMG = imagecreatetruecolor($imgWidth,$imgHeight); $font_type_bold = '/dejavu/DejaVuSansCondensed-Bold.ttf'; $background = imagecolorallocate($IMG,

我有一个字符串数组可以放在页面上,我设法使它水平居中,现在我需要在图像的特定部分垂直居中

     $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 );
$imgWidth=240;
$imgHeight=900;
$IMG=imagecreatetruecolor($imgWidth,$imgHeight);
$font_type_bold='/dejavu/DejaVuSansCondensed bold.ttf';
$background=imagecolorallocate($IMG,78129154);
$text_white=imagecolorallocate($IMG,255255);
$IMG=imagecreatetruecolor($imgWidth,$imgHeight);
$max_lenght_of_title=15;
$special\u issue\u name\u mod=“这是一些要放在页面上并拆分为数组以适应框架宽度的示例文本”;
$text=explode(“\n”,文字包装($special\u-issue\u-name\u-mod,$max\u-lenght\u-of-title));
$line_高度=30;
imageline($IMG,0500240500,$text_-white);
imageline($IMG,010240100,$text_-white);
对于($i=0;$i


我添加了
$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\u of_page=350;

对于($i=0;$i您是说文本要垂直居中显示吗?是的,垂直居中,但不是整个高度的中间,而是页面的标记部分。标记部分的中间使用此图像重新采样($new,$image,$margin_x,0,0,0,$new_w,$new_h,$width,$height);imagejpeg($new,$save,100);这没有帮助,您的方法仅用于一种可能性,如果文本是较长或较短的字符串,则它不在图像的所需部分居中。您的代码不包含此处显示的输出。这意味着输出没有带红色边框和不同背景色的块。首先,实现并更新。这仅用于描述tion,现在我更新了它,以便您能够理解问题。
 $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]);