WordPress之用字母替代图片脚本:LetterAvatar

下面由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>&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 />
 
<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 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);
</script>
</html>

原项目地址:https://github.com/daolavi/LetterAvatar

推荐教程:《WordPress

以上就是WordPress之用字母替代图片脚本:LetterAvatar的详细内容,更多请关注双恒网络其它相关文章!

分享到:



1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » WordPress之用字母替代图片脚本:LetterAvatar

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://sudo1.com/page-qun.html。
  • 会员数(个)
  • 12275资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1364稳定运行(天)

提供最优质的资源集合

立即查看 了解详情