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

资源魔 62 0

上面由WordPress技术栏目给各人引见对于用字母代替图片剧本:LetterAvatar,心愿对需求的冤家有所协助!

基于canvas,经过toDataURL静态天生base64图片。今朝我主题的Gravatar头像,就是行使这个LetterAvatar剧本完成未设置Gravatar头像则读取ALT标签,主动天生首字图片代替默许的头像图片。

c7a7e40f3e27a74e2c9db0b8d99afe3.png

以前已有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_co妹妹ents()) {
$alt = get_co妹妹ent_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.百度.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; });
// Co妹妹onJS 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 Co妹妹onJS 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的具体内容,更多请存眷资源魔其它相干文章!

标签: WordPress wordpress教程 wordpress自学 wordpress技术

抱歉,评论功能暂时关闭!