function _image_imagick_word_wrap_annotation in Imagick 7
Helper funtion to wrap text when it is to long
1 call to _image_imagick_word_wrap_annotation()
- image_imagick_annotate in effects/
imagick.annotate.inc - Annotates an image with text
File
- effects/
imagick.annotate.inc, line 192
Code
function _image_imagick_word_wrap_annotation($image, $draw, $text, $maxWidth) {
$text = trim($text);
$words = preg_split('%\\s%', $text, -1, PREG_SPLIT_NO_EMPTY);
$lines = array();
$i = 0;
$lineHeight = 0;
while (count($words) > 0) {
$metrics = $image
->queryFontMetrics($draw, implode(' ', array_slice($words, 0, ++$i)));
$lineHeight = max($metrics['textHeight'], $lineHeight);
// check if we have found the word that exceeds the line width
if ($metrics['textWidth'] > $maxWidth or count($words) < $i) {
// handle case where a single word is longer than the allowed line width (just add this as a word on its own line?)
if ($i == 1) {
$i++;
}
$lines[] = implode(' ', array_slice($words, 0, --$i));
$words = array_slice($words, $i);
$i = 0;
}
}
return array(
$lines,
$lineHeight,
);
}