ui-avatars icon indicating copy to clipboard operation
ui-avatars copied to clipboard

Center text?

Open HsRealDev opened this issue 6 years ago β€’ 11 comments

Hi,

sorry, my OCD is bugging me again.

Could it be possible to center the text on the image?

Example of an avatar where text is not in aligned to the center: https://ui-avatars.com/api/?name=II&size=250

ps. Awesome service to help out with project bootstrapping.

HsRealDev avatar Oct 17 '17 08:10 HsRealDev

Thank you!

Sure thing, I will attempt to make centering better! πŸ‘

LasseRafn avatar Oct 17 '17 21:10 LasseRafn

Hi Lasse,

I did some more debugging. And as one friend of mine once said: "The sitation is strange." :D

Changing the driver of ImageManager to Imagick in InitialAvatar class ...

InitialAvatar.php, line 34:
$this->image = new ImageManager(['driver' => 'imagick']);

... makes text to get then properly aligned in horizontal but the vertical alignment then goes wrong :)

Bugger.

Maybe there be a bug somewhere in Intervention -> Image lib. Or with the font.

HsRealDev avatar Oct 18 '17 13:10 HsRealDev

And I debugged even a bit more using directly ImageMagick. I did this simple command line script:

<?php
declare(strict_types=1);

$fontFileName = 
    "/tmp/fonts/OpenSans-Regular.ttf";

$fillColor       = new \ImagickPixel('#000');
$strokeColor     = new \ImagickPixel('#000');
$backgroundColor = new \ImagickPixel('#f7f924');

$initials = 'PM';

$avatarSize = 256;
$fontSize   = 0.5 * $avatarSize;

$draw = new \ImagickDraw();
$draw->setStrokeColor($strokeColor);
$draw->setFillColor($fillColor);
$draw->setStrokeWidth(2);
$draw->setFontSize($fontSize);

$draw->setFont(
    $fontFileName
);

$draw->setGravity(\Imagick::GRAVITY_CENTER);
$draw->annotation(0, 0, $initials);

$imagick = new \Imagick();
$imagick->newImage($avatarSize, $avatarSize, $backgroundColor);
$imagick->drawImage($draw);
$imagick->writeImage("/tmp/test.png");

That one also has some problems to place the text correctly "exactly" aligned. But, IMHO, has better general output on different fonts and sizes.

What do you think Lasse? If you feel like this won't fix on UI-avatars, i understand :) In that case, just close this issue.

HsRealDev avatar Oct 18 '17 15:10 HsRealDev

Sorry for the crazy slow response, been super busy. Anyway, I think this makes sense πŸ‘ Will try to experiment and compare the two, maybe add an option to use one, but default to the most correct one

LasseRafn avatar Apr 08 '18 20:04 LasseRafn

Hi this is a really useful service however this off centre issue is starting to bug me too. Is there any update on fixing this?

TomW1605 avatar Sep 29 '19 04:09 TomW1605

I’m open to suggestions but I can’t think of any solutions sadly πŸ‘ŽπŸ»

LasseRafn avatar Sep 29 '19 07:09 LasseRafn

Any solution yet ?

aliaanis avatar Jan 09 '20 11:01 aliaanis

@aliaanis I've been unable to find a solution πŸ‘Ž I will try @HsRealDev's solution as I think he is definitively onto something (good work, thanks!)

LasseRafn avatar Mar 05 '20 15:03 LasseRafn

fwiw: it looks like svg might be rendering correctly and failing during png conversion? svg (https://ui-avatars.com/api/?name=B&size=250&format=svg) image

png (https://ui-avatars.com/api/?name=B&size=250&format=png) image

curtiscook avatar Jun 30 '20 22:06 curtiscook

Any updates on this @LasseRafn ? πŸ™πŸΌ

redac avatar Oct 25 '23 09:10 redac

@LasseRafn did the solution you tried lead anywhere?

redac avatar Feb 29 '24 15:02 redac