首字母头像
/** * 首字母头像 * @param string $text 文本内容 * @param int $whf 图片大小 * @param mixed $rgb 背景rgb,数组或字符串 */ public static function letterAvatar(string $text, int $whf = 100, $rgb = '') { $text = strtoupper(mb_substr($text, 0, 1)); $md5 = substr(md5($text), 2); if (!$rgb) { $rgb = []; for ($i = 0; $i < 3; $i++) { $a = substr($md5, $i * 10, 10); $b = 0; for ($j = 0; $j < 10; $j++) { $b += ord(substr($a, $j, 1)); } $b = intval(($b - 480) * 0.3446); $rgb[] = $b; } } else { if (is_string($rgb)) { $rgb = explode(',', $rgb); } } $bg = "rgb({$rgb[0]},{$rgb[1]},{$rgb[2]})"; $f = intval($whf / 2); $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="' . $whf . '" width="' . $whf . '"><rect fill="' . $bg . '" x="0" y="0" width="' . $whf . '" height="' . $whf . '"></rect><text x="' . $f . '" y="' . $f . '" font-size="' . $f . '" fill="#FFFFFF" style="dominant-baseline:middle;text-anchor:middle">' . $text . '</text></svg>'); $value = 'data:image/svg+xml;base64,' . $src; return $value; }
发表评论