下面由wordpress技巧栏目给大家介绍关于用字母替代图片脚本:letteravatar,希望对需要的朋友有所帮助!
基于canvas,通过todataurl动态生成base64图片。目前我主题的gravatar头像,就是利用这个letteravatar脚本实现未设置gravatar头像则读取alt标签,自动生成首字图片替代默认的头像图片。
之前已有wp爱好者制作了一款:mk-letter-avatar 字母头像插件,试了一下很好用,不过打开浏览器开者工具发现产生大量404错误,看了一下源代码,该插件是通过无头像返回404错误,触发onerror事件用自动生成的字母图片替换src图片地址,判断方式不是很合理,如果不是因为个缺点我都想直接拿来用了,如果作者再优化一下,绝对是款优秀实用的插件。
我的实现原理和插件不同,配合头像本地缓存功能,判断无头像后,直接为无头像的图片添加特定的class类,然后通过letteravatar脚本替换图片。
需要注意的是上面提到的插件,gravatar头像图片必须有alt标签属性,否则不会生成正常的图片,可惜大部分主题默认gravatar头像alt标签属性是空的.....
如果想自动为gravatar头像添加alt标签属性,可以将下面的代码添加到当前主题函数模板functions.php中:
function zm_gravatar_alt($altgravatar) {if (have_comments()) {$alt = get_comment_author();}else {$alt = get_the_author_meta('display_name');}$altgravatar= str_replace('alt=\'\'', 'alt=\'' . $alt . '\' title=\'gravatar for ' . $alt . '\'', $altgravatar);return $altgravatar;}add_filter('get_avatar', 'zm_gravatar_alt');
之后,自动将评论者昵称做为alt属性。
本文只是自己做个记录,并不是教大家怎么弄这个头像,如果认为这字母头像还不错,请直接使用上面介绍的插件。
另附letteravatar脚本演示代码:
<!doctype html><html><h1>letter avatar</h1> <small><strong>用法:</strong></small><pre><code><img src="" class="avatar photo" width="256" height="256" alt="知更鸟" color="#c40000"></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" /> <style>body { font-family: "helvetica neue", helvetica, arial, sans-serif;} pre { margin: 20px 0; padding: 20px; background: #fafafa;} .avatar { border-radius: 50%; }</style><script src="https://libs.baidu.com/jquery/3.2.1/jquery.min.js "></script><script>/** letteravatar* * artur heinze* create letter avatar based on initials* based on https://gist.github.com/leecrossley/6027780*/(function(w, d){function letteravatar (name, size, color) { name = name || '';size = size || 60; var colours = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#ecf0f1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"], namesplit = string(name).split(' '),initials, charindex, colourindex, canvas, context, datauri; if (namesplit.length == 1) {initials = namesplit[0] ? namesplit[0].charat(0):'?';} else {initials = namesplit[0].charat(0) + namesplit[1].charat(0);} if (w.devicepixelratio) {size = (size * w.devicepixelratio);}charindex = (initials == '?' ? 72 : initials.charcodeat(0)) - 64;colourindex = charindex % 20;canvas = d.createelement('canvas');canvas.width = size;canvas.height = size;context = canvas.getcontext("2d"); context.fillstyle = color ? color : colours[colourindex - 1];context.fillrect (0, 0, canvas.width, canvas.height);context.font = math.round(canvas.width/2)+"px arial";context.textalign = "center";context.fillstyle = "#fff";context.filltext(initials, size / 2, size / 1.5); datauri = canvas.todataurl();canvas = null; return datauri;} letteravatar.transform = function() { array.prototype.foreach.call(d.queryselectorall('img[alt]'), function(img, name, color) {name = img.getattribute('alt');color = img.getattribute('color');img.src = letteravatar(name, img.getattribute('width'), color);img.removeattribute('avatar');img.setattribute('alt', name);});}; // amd supportif (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);</script></html>
原项目地址:https://github.com/daolavi/letteravatar
推荐教程:《wordpress》
以上就是wordpress之用字母替代图片脚本:letteravatar的详细内容。