Fix avatar generator centering
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
parent
cd87a40eb3
commit
8e3382ceda
|
@ -275,12 +275,9 @@ class Avatar implements IAvatar {
|
||||||
$font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf';
|
$font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf';
|
||||||
|
|
||||||
$fontSize = $size * 0.4;
|
$fontSize = $size * 0.4;
|
||||||
$box = imagettfbbox($fontSize, 0, $font, $text);
|
|
||||||
|
|
||||||
$x = ($size - ($box[2] - $box[0])) / 2;
|
list($x, $y) = $this->imageTTFCenter($im, $text, $font, $fontSize);
|
||||||
$y = ($size - ($box[1] - $box[7])) / 2;
|
|
||||||
$x += 1;
|
|
||||||
$y -= $box[7];
|
|
||||||
imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text);
|
imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
@ -291,6 +288,35 @@ class Avatar implements IAvatar {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate real image ttf center
|
||||||
|
*
|
||||||
|
* @param resource $image
|
||||||
|
* @param string $text text string
|
||||||
|
* @param string $font font path
|
||||||
|
* @param int $size font size
|
||||||
|
* @param int $angle
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
protected function imageTTFCenter($image, string $text, string $font, int $size, $angle = 0): Array {
|
||||||
|
// Image width & height
|
||||||
|
$xi = imagesx($image);
|
||||||
|
$yi = imagesy($image);
|
||||||
|
|
||||||
|
// bounding box
|
||||||
|
$box = imagettfbbox($size, $angle, $font, $text);
|
||||||
|
|
||||||
|
// imagettfbbox can return negative int
|
||||||
|
$xr = abs(max($box[2], $box[4]));
|
||||||
|
$yr = abs(max($box[5], $box[7]));
|
||||||
|
|
||||||
|
// calculate bottom left placement
|
||||||
|
$x = intval(($xi - $xr) / 2);
|
||||||
|
$y = intval(($yi + $yr) / 2);
|
||||||
|
|
||||||
|
return array($x, $y);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate steps between two Colors
|
* Calculate steps between two Colors
|
||||||
* @param object Color $steps start color
|
* @param object Color $steps start color
|
||||||
|
|
Loading…
Reference in New Issue