用字母替代图片脚本:LetterAvatar(无插件实现)

基于canvas,通过toDataURL动态生成base64图片。目前我主题的Gravatar头像,就是利用这个LetterAvatar脚本实现未设置Gravatar头像则读取ALT标签,自动生成首字图片替代默认的头像图片。

用字母替代图片脚本:LetterAvatar(无插件实现)

之前已有WP爱好者制作了一款:mk-letter-avatar 字母头像插件,试了一下很好用,不过打开浏览器开者工具发现产生大量404错误,看了一下源代码,该插件是通过无头像返回404错误,触发onerror事件用自动生成的字母图片替换src图片地址,判断方式不是很合理,如果不是因为个缺点我都想直接拿来用了,如果作者再优化一下,绝对是款优秀实用的插件。

我的实现原理和插件不同,配合头像本地缓存功能,判断无头像后,直接为无头像的图片添加特定的class类,然后通过LetterAvatar脚本替换图片。

需要注意的是上面提到的插件,Gravatar头像图片必须有alt标签属性,否则不会生成正常的图片,可惜大部分主题默认Gravatar头像alt标签属性是空的…..

如果想自动为Gravatar头像添加alt标签属性,可以将下面的代码添加到当前主题函数模板functions.php中:

  1. function zm_gravatar_alt($altgravatar) {
  2. if (have_comments()) {
  3. $alt = get_comment_author();
  4. }
  5. else {
  6. $alt = get_the_author_meta('display_name');
  7. }
  8. $altgravatar= str_replace('alt=''', 'alt='' . $alt . '' title='Gravatar for ' . $alt . ''', $altgravatar); return $altgravatar; } add_filter('get_avatar', 'zm_gravatar_alt');

之后,自动将评论者昵称做为alt属性。

本文只是自己做个记录,并不是教大家怎么弄这个头像,如果认为这字母头像还不错,请直接使用上面介绍的插件。 展开收缩

另附LetterAvatar脚本演示代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <h1>Letter Avatar</h1> <small><strong>用法:</strong></small> <pre> <code>&lt;img src=&quot;&quot; class=&quot;avatar photo&quot; width=&quot;256&quot; height=&quot;256&quot; alt=&quot;星空站长网&quot; color=&quot;#c40000&quot;&gt;</code> </pre> <img src="" class="avatar photo" height="240" width="240" alt="星空站长网" color="#c40000" /> <img src="" class="avatar photo" height="240" width="240" alt="站长" color="#99CC66" /> <img src="" class="avatar photo" height="240" width="240" alt="网" color="#0D8ABC" />
  4. <style>
  5. body {
  6. font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  7. }
  8. pre {
  9. margin: 20px 0;
  10. padding: 20px;
  11. background: #fafafa;
  12. }
  13. .avatar {
  14. border-radius: 50%;
  15. }
  16. </style>
  17. <script src="https://libs.baidu.com/jquery/3.2.1/jquery.min.js "></script>
  18. <script>
  19. /* * LetterAvatar * * Artur Heinze * Create Letter avatar based on Initials * based on https://gist.github.com/leecrossley/6027780 */
  20. (function(w, d) {
  21. function LetterAvatar(name, size, color) {
  22. name = name || '';
  23. size = size || 60;
  24. var colours = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#ecf0f1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"],
  25. nameSplit = String(name).split(' '),
  26. initials, charIndex, colourIndex, canvas, context, dataURI;
  27. if(nameSplit.length == 1) {
  28. initials = nameSplit[0] ? nameSplit[0].charAt(0) : '?';
  29. } else {
  30. initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0);
  31. }
  32. if(w.devicePixelRatio) {
  33. size = (size * w.devicePixelRatio);
  34. }
  35. charIndex = (initials == '?' ? 72 : initials.charCodeAt(0)) - 64;
  36. colourIndex = charIndex % 20;
  37. canvas = d.createElement('canvas');
  38. canvas.width = size;
  39. canvas.height = size;
  40. context = canvas.getContext("2d");
  41. context.fillStyle = color ? color : colours[colourIndex - 1];
  42. context.fillRect(0, 0, canvas.width, canvas.height);
  43. context.font = Math.round(canvas.width / 2) + "px Arial";
  44. context.textAlign = "center";
  45. context.fillStyle = "#FFF";
  46. context.fillText(initials, size / 2, size / 1.5);
  47. dataURI = canvas.toDataURL();
  48. canvas = null;
  49. return dataURI;
  50. }
  51. LetterAvatar.transform = function() {
  52. Array.prototype.forEach.call(d.querySelectorAll('img[alt]'), function(img, name, color) {
  53. name = img.getAttribute('alt');
  54. color = img.getAttribute('color');
  55. img.src = LetterAvatar(name, img.getAttribute('width'), color);
  56. img.removeAttribute('avatar');
  57. img.setAttribute('alt', name);
  58. });
  59. }; // AMD support if (typeof define === 'function' && define.amd) { define(function () { return LetterAvatar; }); // CommonJS and Node.js module support. } else if (typeof exports !== 'undefined') { // Support Node.js specific `module.exports` (which can be a function) if (typeof module != 'undefined' && module.exports) { exports = module.exports = LetterAvatar; } // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function) exports.LetterAvatar = LetterAvatar; } else { window.LetterAvatar = LetterAvatar; d.addEventListener('DOMContentLoaded', function(event) { LetterAvatar.transform(); }); } })(window, document);
  60. </script>
  61. </html>

给TA打赏
共{{data.count}}人
人已打赏
建站笔记

修正 WordPress 密码设置链接错误

2022-6-7 16:15:54

建站笔记

批量替换WordPress自定义栏目值

2022-6-7 16:17:58

重要说明

本站资源大多来自网络,如有侵犯你的权益请联系管理员,发送邮件   68836010@qq.com  我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果遇到特殊找不到下载链接的文章,或者不能下载,或者解压失败,先不要忙。 有可能有事情或者在睡觉不能及时的回复您,QQ留言后, 请耐心等待即可! QQ:68836010  

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧