Fix avatar generator centering

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
pull/9197/head
John Molakvoæ (skjnldsv) 7 years ago
parent cd87a40eb3
commit 8e3382ceda
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
  1. 36
      lib/private/Avatar.php

@ -275,12 +275,9 @@ class Avatar implements IAvatar {
$font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf';
$fontSize = $size * 0.4;
$box = imagettfbbox($fontSize, 0, $font, $text);
$x = ($size - ($box[2] - $box[0])) / 2;
$y = ($size - ($box[1] - $box[7])) / 2;
$x += 1;
$y -= $box[7];
list($x, $y) = $this->imageTTFCenter($im, $text, $font, $fontSize);
imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text);
ob_start();
@ -291,6 +288,35 @@ class Avatar implements IAvatar {
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
* @param object Color $steps start color

Loading…
Cancel
Save