php-gdlabel/src/Elements/TextElement.php

44 lines
1.2 KiB
PHP

<?php
namespace NoccyLabs\GdLabel\Elements;
use NoccyLabs\GdLabel\Element;
class TextElement extends Element
{
protected function draw($gd, int $x, int $y, int $width, int $height, array $params)
{
if (($bound = $this->getProp("bind-value"))) {
$value = array_key_exists($bound, $params)?$params[$bound]:"?{$bound}?";
} else {
$value = $this->getProp("value");
}
$fontName = $this->getProp("font")?:"liberation sans";
$fontStyle = $this->getProp("font-style")?:"regular";
$font = \NoccyLabs\GdLabel\FontRegistry::getInstance()->findFont($fontName, $fontStyle);
if (!$font) {
return;
}
$color = $this->getPropRgb("text-color");
$size = $this->getProp("font-size", 8);
$dims = imagettfbbox($size, 0, $font, "AaBbXxYyZz0129:;.");
$textAscent = abs($dims[7]);
$textDescent = abs($dims[1]);
$textWidth = abs($dims[0])+abs($dims[2]);
$textHeight = $textAscent+$textDescent;
imagettftext($gd, $size, 0, $x, $y + $textAscent, $color, $font, $value);
//imagestring($gd, $size, $x, $y, $value, $color);
}
}