1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 09:10:21 +02:00

add getBoxSize() for imagick

In addition to #165 it would be great if `imagick` also supports this method.
This commit is contained in:
Tom Witkowski
2017-09-14 15:42:58 +02:00
committed by GitHub
parent 4581c28cdc
commit 42d808bd61

View File

@@ -75,4 +75,44 @@ class Font extends \Intervention\Image\AbstractFont
// apply to image
$image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * (-1), $this->text);
}
/**
* Calculates bounding box of current font setting
*
* @return array
*/
public function getBoxSize()
{
$box = [];
// build draw object
$draw = new \ImagickDraw();
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
// set font file
if ($this->hasApplicableFontFile()) {
$draw->setFont($this->file);
} else {
throw new \Intervention\Image\Exception\RuntimeException(
"Font file must be provided to apply text to image."
);
}
$draw->setFontSize($this->size);
$dimensions = (new \Imagick())->queryFontMetrics($draw, $this->text);
if (strlen($this->text) == 0) {
// no text -> no boxsize
$box['width'] = 0;
$box['height'] = 0;
} else {
// get boxsize
$box['width'] = intval(abs($dimensions['textWidth']));
$box['height'] = intval(abs($dimensions['textHeight']));
}
return $box;
}
}