It turns right-justified, and if for single-line text I can independently set indents ( $ padLeft is left indent), then for two or more lines, I use the standard call for text output:

imagettftext($im, 12, 0, $padLeft, $padTop, $textColor, $font, "строка1 

\ nstring2 ");

  • one
    Working with text in images is always a pain, specifically with gd you must call imagettfbbox () and measure the width of the result. When I got the task of transferring lines and cutting back on length with the substitution of ellipses, I stupidly realized this through the number of characters. With the package imagine / imagine it should be simpler, there seems to be Font :: box () or something like that. - etki

2 answers 2

Output and align each line separately. A working example of such a level is here :

     function text_center ($im, $str, font, $textColor, $pad_left, $padTop, $width_text, $font_size ) { $arr = explode(' ', $str); $ret = ""; foreach($arr as $word){ $tmp_string = $ret.' '.$word; $testbox = imagettfbbox($font_size, 0, $font, $tmp_string); if($testbox[2] > $width_text) $ret.=($ret==""?"":"\n").$word; else $ret.=($ret==""?"":" ").$word; } $arr = explode("\n", $ret); foreach($arr as $str){ $testbox = imagettfbbox($font_size, 0, $font, $str);// Размер строки $left_x = round(($width_text - ($testbox[2] - $testbox[0]))/2); imagettftext($im, $font_size, 0, $left_x +$pad_left, ($padTop), $textColor, $font, $str); $padTop=$padTop+ $font_size*1.5; } }