山东时时彩怎么中奖|十一运夺金山东时时彩|
点击登录
  • 欢迎访问好资源,这里会在第一时间更新最新最好的资源给您,如果您有好的资源、文章,可投稿。如有其他需要请联系 QQ 点我注册
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏好资源吧~ 点我注册

Gravatar 头像被墙及其解决方案

网络技术 itsmalldog 1339次浏览 已收录 1个评论 扫描二维码
本文最后更新于2014年11月18日,已超过 1 年没有更新,如果文章内容或?#35745;?#36164;源失效,请留言反馈,我们会及时处理,谢谢!

采用多种方法解决因为Gravatar被墙而导致的头像文件无法显示的问题,希望对您有用

Gravatar 头像被墙及其解决方案

前言

最近在某个群里看到好多问Gravatar被墙的事,因为WordPress使用了Gravatar的头像,可能会导致网页加载失败,最后还会留下裂图,很是影响用户体验,所以解决头像加载失败的方法刻不容缓。云落从网上收集了一些方法,给大家做个推荐,自己适合那种方法,自己看的办吧。

一、使用https加载

因为发现Gravatar还没死绝,https的还可以访问,所以使用https访问的也算是一种方案。在functions.php文件内添加以下代码

<span class="kwd"><span class="kwd">function</span></span><span class="pln"><span class="pln"> get_ssl_avatar</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">{</span></span><span class="pln"><span class="pln">
$avatar </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> preg_replace</span></span><span class="pun"><span class="pun">(</span></span><span class="str"><span class="str">'/.*\/avatar\/(.*)\?s=([\d]+)&.*/'</span></span><span class="pun"><span class="pun">,</span></span><span class="str"><span class="str">'<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">'</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">);</span></span>
<span class="kwd"><span class="kwd">return</span></span><span class="pln"><span class="pln"> $avatar</span></span><span class="pun"><span class="pun">;</span></span>
<span class="pun"><span class="pun">}</span></span><span class="pln"><span class="pln">
add_filter</span></span><span class="pun"><span class="pun">(</span></span><span class="str"><span class="str">'get_avatar'</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">'get_ssl_avatar'</span></span><span class="pun"><span class="pun">);</span></span>
注意:使用这个方法的前提条件是https能够访问
优点:简单稳定
缺点: 当头像特别大时速度可能?#26376;?/div>

二、WordPress本地缓存

functions.php文件插入以下代码

<span class="kwd"><span class="kwd">function</span></span><span class="pln"><span class="pln"> my_avatar</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">{</span></span><span class="pln"><span class="pln">
$tmp </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> strpos</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">'http'</span></span><span class="pun"><span class="pun">);</span></span><span class="pln"><span class="pln">
$g </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> substr</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> strpos</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">"'"</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">-</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">);</span></span><span class="pln"><span class="pln">
$tmp </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> strpos</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$g</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">'avatar/'</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">+</span></span> <span class="lit"><span class="lit">7</span></span><span class="pun"><span class="pun">;</span></span><span class="pln"><span class="pln">
$f </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> substr</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$g</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> strpos</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$g</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">"?"</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">-</span></span><span class="pln"><span class="pln"> $tmp</span></span><span class="pun"><span class="pun">);</span></span><span class="pln"><span class="pln">
$w </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> get_bloginfo</span></span><span class="pun"><span class="pun">(</span></span><span class="str"><span class="str">'wpurl'</span></span><span class="pun"><span class="pun">);</span></span><span class="pln"><span class="pln">
$e </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> ABSPATH </span></span><span class="pun"><span class="pun">.</span></span><span class="str"><span class="str">'avatar/'</span></span><span class="pun"><span class="pun">.</span></span><span class="pln"><span class="pln"> $f </span></span><span class="pun"><span class="pun">.</span></span><span class="str"><span class="str">'.jpg'</span></span><span class="pun"><span class="pun">;</span></span><span class="pln"><span class="pln">
$t </span></span><span class="pun"><span class="pun">=</span></span> <span class="lit"><span class="lit">1209600</span></span><span class="pun"><span class="pun">;</span></span> <span class="com"><span class="com">//設定14天, 單位:秒</span></span>
<span class="kwd"><span class="kwd">if</span></span> <span class="pun"><span class="pun">(</span></span> <span class="pun"><span class="pun">!</span></span><span class="pln"><span class="pln">is_file</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$e</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">||</span></span> <span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">time</span></span><span class="pun"><span class="pun">()</span></span> <span class="pun"><span class="pun">-</span></span><span class="pln"><span class="pln"> filemtime</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$e</span></span><span class="pun"><span class="pun">))</span></span> <span class="pun"><span class="pun">></span></span><span class="pln"><span class="pln"> $t </span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">{</span></span> <span class="com"><span class="com">//當頭像不存在或文件超過14天才更新</span></span><span class="pln"><span class="pln">
copy</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">htmlspecialchars_decode</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$g</span></span><span class="pun"><span class="pun">),</span></span><span class="pln"><span class="pln"> $e</span></span><span class="pun"><span class="pun">);</span></span>
<span class="pun"><span class="pun">}</span></span> <span class="kwd"><span class="kwd">else</span></span><span class="pln"><span class="pln">  $avatar </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> strtr</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> array</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$g </span></span><span class="pun"><span class="pun">=></span></span><span class="pln"><span class="pln"> $w</span></span><span class="pun"><span class="pun">.</span></span><span class="str"><span class="str">'/avatar/'</span></span><span class="pun"><span class="pun">.</span></span><span class="pln"><span class="pln">$f</span></span><span class="pun"><span class="pun">.</span></span><span class="str"><span class="str">'.jpg'</span></span><span class="pun"><span class="pun">));</span></span>
<span class="kwd"><span class="kwd">if</span></span> <span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">filesize</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$e</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun"><</span></span> <span class="lit"><span class="lit">500</span></span><span class="pun"><span class="pun">)</span></span><span class="pln"><span class="pln"> copy</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$w</span></span><span class="pun"><span class="pun">.</span></span><span class="str"><span class="str">'/avatar/default.jpg'</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> $e</span></span><span class="pun"><span class="pun">);</span></span>
<span class="kwd"><span class="kwd">return</span></span><span class="pln"><span class="pln"> $avatar</span></span><span class="pun"><span class="pun">;</span></span>
<span class="pun"><span class="pun">}</span></span><span class="pln"><span class="pln">
add_filter</span></span><span class="pun"><span class="pun">(</span></span><span class="str"><span class="str">'get_avatar'</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">'my_avatar'</span></span><span class="pun"><span class="pun">);</span></span>

在WordPress安?#26696;?#30446;(即跟wp-content等文件夹同一目录下)下新建一个名为 avatar 的文件夹 ,并给予写权限。放入一张同尺寸的头像?#35745;?#21629;名为default.jpg。

优点:和主机速度挂?#24120;?#22914;果你的服务器特别快则头像加载也特别快
缺点:只能缓存一个尺寸的头像,国内主机无法使用

三、第三方缓存

这里说的第三方主要指的是多说,在functions.php文件插入以下代码

<span class="kwd"><span class="kwd">function</span></span><span class="pln"><span class="pln"> duoshuo_avatar</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">)</span></span> <span class="pun"><span class="pun">{</span></span><span class="pln"><span class="pln">
$avatar </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> str_replace</span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">array</span></span><span class="pun"><span class="pun">(</span></span><span class="str"><span class="str">"www.gravatar.com"</span></span><span class="pun"><span class="pun">,</span></span><span class="str"><span class="str">"0.gravatar.com"</span></span><span class="pun"><span class="pun">,</span></span><span class="str"><span class="str">"1.gravatar.com"</span></span><span class="pun"><span class="pun">,</span></span><span class="str"><span class="str">"2.gravatar.com"</span></span><span class="pun"><span class="pun">),</span></span><span class="str"><span class="str">"gravatar.duoshuo.com"</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln">$avatar</span></span><span class="pun"><span class="pun">);</span></span>
<span class="kwd"><span class="kwd">return</span></span><span class="pln"><span class="pln"> $avatar</span></span><span class="pun"><span class="pun">;</span></span>
<span class="pun"><span class="pun">}</span></span><span class="pln"><span class="pln">
add_filter</span></span><span class="pun"><span class="pun">(</span></span> <span class="str"><span class="str">'get_avatar'</span></span><span class="pun"><span class="pun">,</span></span> <span class="str"><span class="str">'duoshuo_avatar'</span></span><span class="pun"><span class="pun">,</span></span> <span class="lit"><span class="lit">10</span></span><span class="pun"><span class="pun">,</span></span> <span class="lit"><span class="lit">3</span></span> <span class="pun"><span class="pun">);</span></span>

好资源 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Gravatar 头像被墙及其解决方案
?#19981;?(3)
[捐赠网站,支持好资源!]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 不错 很厉害 个人很?#19981;?div class="c-meta">澳门银座总代2016-07-15 23:03 回复 Windows 10 | 搜狗浏览器 2.X
山东时时彩怎么中奖