1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23: | function text_restrict_wordsize($str, $size, $fontsize = 7, $fontfile = 'verdana.ttf') {
$size = max($size, 10);
$fontpath = $_SERVER['DOCUMENT_ROOT'] . '/source/fonts/' . $fontfile;
$strings = explode(' ', $str);
foreach ($strings as $key => $string ) {
$dimensions = imagettfbbox($fontsize, 0, $fontpath, $string);
$width = $dimensions[4]-$dimensions[6];
if ($width > $size) {
while ($width > $size) {
$string = substr($string, 0, strlen($string)-1);
$dimensions = imagettfbbox($fontsize, 0, $fontpath, $string . '-');
$width = $dimensions[4]-$dimensions[6];
}
$strings[$key] = $string . '- ' . text_restrict_wordsize(substr($strings[$key], strlen($string), strlen($strings[$key]) - strlen($string) - 1), $size, $fontsize, $fontfile);
}
}
return implode(' ', $strings);
} |