private function Annotate::_image_imagick_word_wrap_annotation in Imagick 8
Helper funtion to wrap text when it is to long
Parameters
Imagick $image:
ImagickDraw $draw:
string $text:
int $maxWidth:
Return value
array
File
- src/
Plugin/ ImageToolkit/ Operation/ imagick/ Annotate.php, line 108
Class
- Annotate
- Defines imagick annotate operation.
Namespace
Drupal\imagick\Plugin\ImageToolkit\Operation\imagickCode
private function _image_imagick_word_wrap_annotation($image, $draw, $text, $maxWidth) {
$text = trim($text);
$words = preg_split('%\\s%', $text, -1, PREG_SPLIT_NO_EMPTY);
$lines = [];
$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 [
$lines,
$lineHeight,
];
}