JQ给typecho打造字母头像

偶然发现码云上有个非常人性化的细节:会自动给没头像的用户生成一个昵称首字符的彩色头像

实现纯前端生成字母头像

偶然发现码云上有个非常人性化的细节:会自动给没头像的用户生成一个昵称首字符的彩色头像,关键是打开控制台一看,发现这头像居然还是在前端实时生成的 这就很有意思了! !....

发现这头像居然还是在前端实时生成的 这就很有意思了!

455392163.png

它使用的是一个叫 LetterAvatar 的 JS 插件。它的原理是利用动态创建的 canvas 生成图像,然后显示在 img 标签中。

JS代码:

  1. /**
  2. * LetterAvatar
  3. *
  4. * Artur Heinze
  5. * Create Letter avatar based on Initials
  6. * based on https://gist.github.com/leecrossley/6027780
  7. */
  8. (function(w, d){
  9. function LetterAvatar (name, size, color) {
  10. name = name || '';
  11. size = size || 80;
  12. var colours = [
  13. "#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50",
  14. "#f1c40f", "#e67e22", "#e74c3c", "#00bcd4", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"
  15. ],
  16. nameSplit = String(name).split(' '),
  17. initials, charIndex, colourIndex, canvas, context, dataURI;
  18. if (nameSplit.length == 1) {
  19. initials = nameSplit[0] ? nameSplit[0].charAt(0):'?';
  20. } else {
  21. initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0);
  22. }
  23. if (w.devicePixelRatio) {
  24. size = (size * w.devicePixelRatio);
  25. }
  26. charIndex = (initials == '?' ? 72 : initials.charCodeAt(0)) - 64;
  27. colourIndex = charIndex % 20;
  28. canvas = d.createElement('canvas');
  29. canvas.width = size;
  30. canvas.height = size;
  31. context = canvas.getContext("2d");
  32. context.fillStyle = color ? color : colours[colourIndex - 1];
  33. context.fillRect (0, 0, canvas.width, canvas.height);
  34. context.font = Math.round(canvas.width/2)+"px 'Microsoft Yahei'";
  35. context.textAlign = "center";
  36. context.fillStyle = "#FFF";
  37. context.fillText(initials, size / 2, size / 1.5);
  38. dataURI = canvas.toDataURL();
  39. canvas = null;
  40. return dataURI;
  41. }
  42. LetterAvatar.transform = function() {
  43. Array.prototype.forEach.call(d.querySelectorAll('img[avatar]'), function(img, name, color) {
  44. name = img.getAttribute('avatar');
  45. color = img.getAttribute('color');
  46. img.src = LetterAvatar(name, img.getAttribute('width'), color);
  47. img.removeAttribute('avatar');
  48. img.setAttribute('alt', name);
  49. });
  50. };
  51. // AMD support
  52. if (typeof define === 'function' && define.amd) {
  53. define(function () { return LetterAvatar; });
  54. // CommonJS and Node.js module support.
  55. } else if (typeof exports !== 'undefined') {
  56. // Support Node.js specific `module.exports` (which can be a function)
  57. if (typeof module != 'undefined' && module.exports) {
  58. exports = module.exports = LetterAvatar;
  59. }
  60. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  61. exports.LetterAvatar = LetterAvatar;
  62. } else {
  63. window.LetterAvatar = LetterAvatar;
  64. d.addEventListener('DOMContentLoaded', function(event) {
  65. LetterAvatar.transform();
  66. });
  67. }
  68. })(window, document);

把这段代码放到自己的主题js里面就可以了,然后再修改主题的头像功能,比如我这边的是

  1. /* 解析头像 */
  2. function getGravatar($mail)
  3. {
  4. $a = Typecho_Widget::widget('Widget_Options')->JGravatars;
  5. $b = 'https://' . $a . '/';
  6. $c = strtolower($mail); //转为小写
  7. $d = md5($c);
  8. $f = str_replace('@qq.com', '', $c);
  9. if (strstr($c, "qq.com") && is_numeric($f) && strlen($f) < 11 && strlen($f) > 4) {
  10. $g = '//thirdqq.qlogo.cn/g?b=qq&nk=' . $f . '&s=100';
  11. } else {
  12. //$g = $b . $d . '?d=mm';
  13. $g = '-1'; //不再输出Gravatar的头像,换成Letter Avatar头像
  14. }
  15. return $g;
  16. }

以上是如果为正常的qq邮箱,则输出qq头像,如果不是qq邮箱,则输出$g=-1

然后再经过这一段

  1. function get_tx($mail){
  2. $tx = getGravatar($mail);
  3. $name = get_name($mail);
  4. if($tx=='-1'){
  5. return '<img class="flex-avatar me-3" avatar="'.$name.'">'; //Letter Avatar头像
  6. }
  7. else{
  8. return '<img src="'.$tx.'" srcset="'.$tx.'" class="avatar">';
  9. }
  10. }

判断是否为$g=-1,-1的时候则显示Letter Avatar头像,否则显示QQ邮箱头像

以上的getGravatar获取qq头像,get_name获取用户昵称

最后

前端调用头像代码:

  1. <?php echo get_tx($this->author->mail); ?>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1070
4 条评论
5.1k

发表评论

已有 4 条评论

  1. 叫啥名好     MacOS /    Chrome
    2022-07-12 04:35

    博主博主,get_name函数有模板吗

    1. 【管理员】Vv     Win 10 /    Chrome
      2022-08-11 11:34

      @叫啥名好

      大概这样
      $postnum=$db->fetchRow($db->select()->from ('table.comments')->where ('mail=?',$mail));
      return $postnum['author'];

    2. 烟络     Android /    Chrome
      2022-08-04 00:01

      @叫啥名好

      兄弟,你找到getname了吗

  2. 若志奕鑫     Win 10 /    Chrome
    2021-10-10 00:03

    学到了

!