Hi @haribonda
Thanks for your recommendations, I think number one looks very nice and it would be a cool feature to add.
What i didn’t understand was the exact problem you are facing while using custom backgrounds. The avatar background color doesn’t update if you change it in settings page?
To accomplish this no so bright color request you can do something like code below. I copied some code from this library https://github.com/mistic100/RandomColor.php which does exactly the same thing as the one you sent. Modify the code to fit your needs.
function my_avatar_url_args( $url_args, $id_or_email ) {
$bg = $url_args['background'];
// Convert the username into a number based on the ASCII value of each
// character.
$num = 0;
for ( $i = 0; $i < strlen( $bg ); $i ++ ) {
$num += ord( substr( $bg, $i ) );
}
// Construct a color using the remainder of that number divided by 360, and
// some predefined saturation and value values.
$hue = $num % 360;
$url_args['background'] = hsv2rgb( $hue, 30, 90 );
$url_args['color'] = leira_letter_avatar()->public->get_contrast_color( $url_args['background'] );
return $url_args;
}
/**
* https://github.com/mistic100/RandomColor.php/blob/394b2a6f87b7cdc2ae0bfb02dd59b9e35fd08d8a/src/RandomColor.php
*
* @param $h
* @param $s
* @param $v
*
* @return string
*/
function hsv2rgb( $h, $s, $v ) {
$h /= 360;
$s /= 100;
$v /= 100;
$i = floor( $h * 6 );
$f = $h * 6 - $i;
$m = $v * ( 1 - $s );
$n = $v * ( 1 - $s * $f );
$k = $v * ( 1 - $s * ( 1 - $f ) );
$r = 1;
$g = 1;
$b = 1;
switch ( $i ) {
case 0:
list( $r, $g, $b ) = array( $v, $k, $m );
break;
case 1:
list( $r, $g, $b ) = array( $n, $v, $m );
break;
case 2:
list( $r, $g, $b ) = array( $m, $v, $k );
break;
case 3:
list( $r, $g, $b ) = array( $m, $n, $v );
break;
case 4:
list( $r, $g, $b ) = array( $k, $m, $v );
break;
case 5:
case 6:
list( $r, $g, $b ) = array( $v, $m, $n );
break;
}
$res = array(
'r' => floor( $r * 255 ),
'g' => floor( $g * 255 ),
'b' => floor( $b * 255 ),
);
$hex = '';//'#'
foreach ( $res as $c ) {
$hex .= str_pad( dechex( $c ), 2, '0', STR_PAD_LEFT );
}
return $hex;
}
add_filter( 'leira_letter_avatar_url_args', 'my_avatar_url_args', 10, 2 );
Exactly as you described SVG is the one that take less space among all formats.
I have no idea how to decrease svg size, first think that comes to my mind is make the xml shorter. You can try something like this as a first approach, let me know how it works for you.
function my_image_content( $avatar, $data ) {
if ( $data['format'] == 'svg' ) {
//create a custom avatar or replace something int he current one.
$avatar = str_replace( "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif", "'Times New Roman', Times, serif", $avatar );
}
return $avatar;
}
add_filter( 'leira_letter_avatar_image_content', 'my_image_content', 10, 2 );