wrap and center text in gd and php -
i have line of text need wrap , center in gd image. using ttf font well. can give me assistance please?
i have managed text wrap doing following, need center:
function wrap($fontsize, $angle, $fontface, $string, $width){ $ret = ""; $arr = explode(' ', $string); foreach ( $arr $word ){ $teststring = $ret.' '.$word; $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststring); if ( $testbox[2] > $width ){ $ret.=($ret==""?"":"\n").$word; } else { $ret.=($ret==""?"":' ').$word; } } return $ret; }
to center both horizontally , vertically: half height imagettfbbox of whole text (with new lines) , substract half height of image ($start_x
).
now split text new lines, create ttfbox every line , it's height ($h
) , half width ($w
). draw line starting half image width + $w
, $start_x
, add $h
$start_x
, repat until lines written.
Comments
Post a Comment