我喜欢的人头像显示在我的博客评论谁. WordPress的支持的gravatar服务本身因此启用,这是很容易. 然而,它是有代价 - 从另一个域访问外部文件中增加了许多额外的加载时间的新形式 DNS 查找, 新 SSL 连接使, 等等. 将 gravatar 存储在本地并从您自己的服务器提供服务不是很好吗. 嗯,这就是我一直在做了一段时间, 如果你想知道如何, 阅读...
In case you’re still wondering just why you’d want to do this, let me offer a few more benefits — images served locally can be compressed before serving them, for example ‑all my images are converted into webp versions, and any browser which supports webp gets the webp version. This is in addition to the reduction from not having to connect to an external server. The local resources can also have a caching header set on them so that browsers will cache them. Gravatar doesn’t provide for a very long cache time.
I have created 3 custom functions, 我只是把我的主题的functions.php文件. The first is a custom filter for the native wordpress get_avatar(). 从的gravatar和谷歌的第二争夺化身呼吁时, and the third creates a daily cronjob which refreshes the gravatars — in case they’ve been changed, or a previously unavailable one is now available.
You will need to do a little bit of tinkering to make it compatible with your theme — I use the “bones” framework as you’ll notice in the 3rd function I’ve reused some code that it provided. You will also need to create, or customise the path to store the gravatars, which in my case are stored in the theme folder in the subpath “/library/images/gravatars/”
/*********************************************\ * 过滤get_avatar只使用本地替身 * \*********************************************/ 功能bones_gravatar($头像, $id_or_email, $尺寸, $默认, $ALT) { $root_path = get_template_directory_uri() . '/库/图像/ gravatars /'; $root_path_local = get_template_directory() . '/库/图像/ gravatars /'; $gravatar_path = $ root_path . 'default_avatar'; $gravatar_path_hidpi =“数据的gravatar-hidpi ="”。$ root_path . “default_avatar-hidpi.png"“; //shamelessly reuse original code to get the e-mail address $email = ''; 如果 ( is_numeric($id_or_email) ) { $ID = (INT) $id_or_email; $用户= get_userdata($ID); 如果 ( $用户 ) $电子邮件= $用户>USER_EMAIL; } ELSEIF ( is_object($id_or_email) ) { // No avatar for pingbacks or trackbacks $allowed_comment_types = apply_filters( 'get_avatar_comment_types', 排列( '评论' ) ); 如果 ( ! 空( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (排列) $allowed_comment_types ) ) 返回false; 如果 ( !空($id_or_email->用户名) ) { $ID = (INT) $id_or_email->用户名; $用户= get_userdata($ID); 如果 ( $用户) $电子邮件= $用户>USER_EMAIL; } ELSEIF ( !空($id_or_email->comment_author_email) ) { $电子邮件= $ id_or_email->comment_author_email; } } 其他 { $电子邮件= $ id_or_email; } 如果 ( !空($电子邮件) ) { $email_hash = MD5( 用strtolower( 修剪( $电子邮件 ) ) ); 如果(文件已存在($root_path_local . $email_hash . “巴纽”)) { //if we have a local cache then use it $gravatar_path= $root_path . $email_hash ; $gravatar_path_hidpi =“数据的gravatar-hidpi ="”。$ root_path . $email_hash . “-hidpi.png"“; } } 如果($尺寸 >= 47) $头像='<IMG类="负载的gravatar头像头像 - “ $大小。”照片" 宽度="'$大小。'" 高度="'$大小。'" SRC ="“ . $gravatar_path . “-hidpi.png" ALT ="的gravatar" />“; 否则$头像='<IMG类="负载的gravatar头像头像 - “ $大小。”照片" 宽度="'$大小。'" 高度="'$大小。'" SRC ="“ . $gravatar_path . ”巴纽" ALT ="的gravatar" “ . $gravatar_path_hidpi . “/>“; 返回$头像; } 的add_filter('get_avatar', 'bones_gravatar', 10, 5); /***********************\ * 创建的gravatar缓存 * \***********************/ 功能grab_avatar($COMMENT_ID,$评论,$刷新= FALSE,$电子邮件=“”) { usleep(20); $root_path = get_template_directory_uri() . '/库/图像/ gravatars /'; $root_path_local = get_template_directory() . '/库/图像/ gravatars /'; 如果($刷新==假) $img_name = MD5( get_comment_author_email($COMMENT_ID) ); 否则$ img_name = MD5( $电子邮件 ); 如果(!文件已存在($root_path_local . $img_name . “巴纽”) || $刷新==真) { 如果($刷新==假) $bgauthemail = get_comment_author_email(); 否则$ bgauthemail = $电子邮件; //try google first $domain= explode("@",$bgauthemail); 如果($域[1]=="gmail.com") { $CH = curl_init(); curl_setopt($CH, CURLOPT_SSL_VERIFYPEER, 假); curl_setopt($CH, CURLOPT_RETURNTRANSFER, true); curl_setopt($CH, CURLOPT_URL, "HTTPS://picasaweb.google.com/data/entry/api/user/" . $bgauthemail . "?ALT = JSON"); $结果= curl_exec($CH); curl_close($CH); $OBJ = json_decode($结果,true); $avatar_from_gmail = $ OBJ['条目'][“GPhoto $缩略图”]['$ T']; $延长= strrchr($avatar_from_gmail, ''); $CH = curl_init($avatar_from_gmail); curl_setopt( $CH, CURLOPT_NOBODY, true ); curl_setopt( $CH, CURLOPT_RETURNTRANSFER, 假 ); curl_setopt( $CH, CURLOPT_HEADER, 假 ); curl_setopt( $CH, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $CH, CURLOPT_MAXREDIRS, 3 ); curl_exec( $CH ); $标题= curl_getinfo( $CH ); curl_close( $CH ); 如果($头['HTTP_CODE'] === 200) { $CH = curl_init($avatar_from_gmail); $FP =的fopen($root_path_local . $img_name . '-hidpi' . $延期, 'WB'); curl_setopt($CH,CURLOPT_USERAGENT,“的Mozilla / 5.0 (Windows NT的 6.1; Win64平台; x64的) 为AppleWebKit / 537.36 (KHTML, 像壁虎) 镀铬/ 39.0.2171.62的Safari / 537.36'); curl_setopt($CH, CURLOPT_FILE, $FP); curl_setopt($CH, CURLOPT_HEADER, 0); curl_exec($CH); curl_close($CH); FCLOSE($FP); $usegravatar = FALSE; $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi' . $延期); 如果 ( ! is_wp_error( $small_image ) ) { //检查一下是不是空白的男人形象, 如果是这样, delete it if (md5_file($root_path_local . $img_name . '-hidpi.jpg') ==用strtolower("686E5C46776BA0E5C488853C1C0B492C")) { //delete unlink($root_path_local . $img_name . '-hidpi.jpg'); //try gravatar $usegravatar=true; } 否则,如果 (md5_file($root_path_local . $img_name . '-hidpi.jpg') ==用strtolower("6D4083BE95FB32358A5110F5A83B9979")) { //delete unlink($root_path_local . $img_name . '-hidpi.jpg'); //try gravatar $usegravatar=true; } 其他 { //always convert to png because some plugins expect all gravatars to be pngs $small_image->保存($root_path_local . $img_name . '-hidpi.png','图像/ PNG'); $small_image->调整(40, 40, true); $small_image->set_quality( 10 ); $small_image->保存($root_path_local . $img_name . “巴纽”,'图像/ PNG'); 如果($扩展==".jpg") 取消链接($root_path_local . $img_name . '-hidpi.jpg'); } } } } //check e-mail isnt a blank generic one else if($bgauthemail!=="noemail@intensedebate.com" || $usegravatar ==真) { $avatar_from_gravatar = "HTTPS://www.gravatar.com/avatar/" . $img_name . "?S = 80&d = 404"; $CH = curl_init($avatar_from_gravatar); curl_setopt( $CH, CURLOPT_NOBODY, true ); curl_setopt( $CH, CURLOPT_RETURNTRANSFER, 假 ); curl_setopt( $CH, CURLOPT_HEADER, 假 ); curl_setopt( $CH, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $CH, CURLOPT_MAXREDIRS, 3 ); curl_exec( $CH ); $标题= curl_getinfo( $CH ); curl_close( $CH ); 如果($头['HTTP_CODE'] === 200) { $CH = curl_init($avatar_from_gravatar); $FP =的fopen($root_path_local . $img_name . '-hidpi.png', 'WB'); curl_setopt($CH,CURLOPT_USERAGENT,“的Mozilla / 5.0 (Windows NT的 6.1; Win64平台; x64的) 为AppleWebKit / 537.36 (KHTML, 像壁虎) 镀铬/ 39.0.2171.62的Safari / 537.36'); curl_setopt($CH, CURLOPT_FILE, $FP); curl_setopt($CH, CURLOPT_HEADER, 0); curl_exec($CH); curl_close($CH); FCLOSE($FP); $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi.png'); 如果 ( ! is_wp_error( $small_image ) ) { $small_image->调整(40, 40, true); $small_image->保存($root_path_local . $img_name . “巴纽”); } } } } } ADD_ACTION('wp_insert_comment', 'grab_avatar'); /********************************************\ * 每天刷新在后台缓存的gravatar * \********************************************/ ADD_ACTION( 'WP', 'bones_setup_schedule' ); 功能bones_setup_schedule() { 如果 ( ! wp_next_scheduled( 'bones_daily_event' ) ) { wp_schedule_event( 时间(), '日常', 'bones_daily_event'); } } ADD_ACTION( 'bones_daily_event', 'bones_refresh_gravatars' ); 功能bones_refresh_gravatars() { //get list of gravatars somehow global $wpdb; $comment_author_emails = $ wpdb->get_results( "SELECT DISTINCT(comment_author_email) 从 " . $wpdb->字首 . "注释" ); 的foreach($comment_author_emails为$关键=> $VAL) { 的foreach($VAL为$ a => $b) { grab_avatar('','',true,$b); } } }
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”