2018年09月
Yii 2 将 Gravatar 头像图片做本地化缓存
Gravatar 是全球公认头像,但头像图片有时候访问奇慢无比,将整个页面的加载速度降了下来,如果能将头像图片缓存到本地,并设置过期时间,那性能会飞速提高,以下代码是 Yii2 的自定义 Helper 中的代码片断,用于实现 Gravatar 本地化访问。
public function gravatar($email, $s = 80, $atts = [], $d = 'mp', $r = 'g') { $key = md5(strtolower(trim($email))); $img_file = Yii::getAlias("@webroot/uploads/avatar/$key.jpg"); $img_url = Yii::getAlias("@web/uploads/avatar/$key.jpg"); $noavatar_file = Yii::getAlias('@webroot/images/noavatar.jpg'); $noavatar_url = Yii::getAlias('@web/images/noavatar.jpg'); // 缓存时间 30 天,超过 30 天会重新到 gravatar.com 去取 if (!file_exists($img_file) || filemtime($img_file) < time() - 3600 * 24 * 30) { file_put_contents($img_file, file_get_contents("https://www.gravatar.com/avatar/$key?s=$s&d=$d&r=$r")); } // 没有设置过头像的用户,显示自定义头像 if (md5_file($img_file) == md5_file($noavatar_file)) { $img_url = $noavatar_url; } return Html::img($img_url, $atts); }
文章归档
文章日历
2018 年 09 月 | ||||||
---|---|---|---|---|---|---|
日 | 一 | 二 | 三 | 四 | 五 | 六 |
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
文章标签
- Linux
- Go
- Yii
- 新浪
- CentOS
- PHP
- Git
- WSL
- Composer
- Mac
- 入职
- Bootstrap
- pyenv
- UCenter
- 厦门
- 出差
- 长沙
- 湖南卫视
- 微博
- Tengine
- YUI
- 泰国
- pecl
- 优化
- GitLab
- 迁移
- rootless
- 年会
- 生日
- Tengin
- RedHat
- Sphinx
- cygwin
- Windows
- Tmux
- Zsh
- 升级
- MySQL
- sql_mode
- Shadowsockets
- 面向对象
- HTTP
- 状态码
- grep
- unoconv
- PPT
- Nginx
- htpasswd
- golang
最新评论
-
崔亮 于 19-05-23 评论了Tengine 2.2.2 配置 http2 协议出现的坑
-
王媛 于 19-01-31 评论了Yii China 是官方唯一认可的中文社区
-
三杯蛙 于 19-01-24 评论了Go 语言中 json 的操作以及常见问题
-
三杯蛙 于 19-01-24 评论了新浪赴泰国曼谷和芭提雅团建
-
巡洋舰 于 18-09-05 评论了CentOS 安装 PHP7
-
小不点 于 11-03-02 评论了WordPress 如何控制每页显示的条数
-
白领厕工 于 10-12-22 评论了关于 phpMyAdmin 免输入用户名和密码,直接进入管理界面
-
清晨的欣赏 于 10-08-04 评论了PHP 5.4 的新特性
-
易赛官网 于 10-07-27 评论了PHP 5.4 的新特性
-
文秘之家 于 10-07-17 评论了PHP 5.4 的新特性
常用工具
- 时间戳转换
- URL ENCODE/DECODE
- JSON
- 正则测试