1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 04:01:30 +02:00

Merge pull request #779 from Gummibeer/patch-1

add getBoxSize() for imagick
This commit is contained in:
Oliver Vogel
2017-09-21 18:33:42 +02:00
committed by GitHub
2 changed files with 47 additions and 0 deletions

View File

@@ -62,6 +62,13 @@ abstract class AbstractFont
* @return boolean
*/
abstract public function applyToImage(Image $image, $posx = 0, $posy = 0);
/**
* Calculates bounding box of current font setting
*
* @return array
*/
abstract public function getBoxSize();
/**
* Create a new instance of Font

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;
}
}