diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-04-16 10:48:34 +0200 |
---|---|---|
committer | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-06-13 18:46:04 +0200 |
commit | 8e3382cedaecf551c3d6991feae08c57f66496eb (patch) | |
tree | e3733a20732efe77b6d81e4b34bafcf417e59192 /lib/private/Avatar.php | |
parent | cd87a40eb3a2b7026dfd1822e6e43e131edd3423 (diff) | |
download | nextcloud-server-8e3382cedaecf551c3d6991feae08c57f66496eb.tar.gz nextcloud-server-8e3382cedaecf551c3d6991feae08c57f66496eb.zip |
Fix avatar generator centering
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'lib/private/Avatar.php')
-rw-r--r-- | lib/private/Avatar.php | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php index 53dea5b966a..9524b36f8e4 100644 --- a/lib/private/Avatar.php +++ b/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(); @@ -292,6 +289,35 @@ class Avatar implements IAvatar { } /** + * 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 * @param object Color $ends end color |