首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取默认用户图片Drupal6

问获取默认用户图片Drupal6
EN

Stack Overflow用户
提问于 2015-04-30 02:18:47
回答 1查看 102关注 0票数 0

我在我的drupal网站上有一个Facebook风格的聊天,但是脚本缺乏用户图片集成。我设法获得了用户图片,但是如果用户没有设置图片,并且使用的是默认图片,我会在日志中得到一个损坏的图片和一个404 not found。我曾尝试为没有图片集的用户获取gravatar,但无法使其工作。

代码语言:javascript
复制
/**
 * This function returns the URL of the avatar of the specified user ID.
 *
 * @param userid the user ID of the user
 * @param image if the image includes more than just a user ID, this param is passed
 * in from the avatar row in the buddylist and get user details functions.
 * @return the link of the user ID's profile
 */
 function get_avatar($image, $user_id, $account) {
   return "http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";
   if (empty($account->picture)) {
         return "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
   }
}
EN

回答 1

Stack Overflow用户

发布于 2015-04-30 04:31:50

你的问题是,你立即返回你的图像,无论它是否在那里:

代码语言:javascript
复制
 function get_avatar($image, $user_id, $account) 
 {
   //THE NEXT LINE RETURNS FROM THE FUNCTION REGARDLESS
   return "http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";

   //THIS CODE NEVER RUNS
   if (empty($account->picture)) {
     return "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
   }
 }

您需要做的是:

代码语言:javascript
复制
 function get_avatar($image, $user_id, $account) 
 {
   $imgurl ="http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";

   if (!is_imgurl_good($imgurl)) {
     $imgurl = "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
   }
   return $imgurl;
 }

在上面的代码中,您必须编写一个函数is_imgurl_good,该函数返回一个指示图像是否良好的boolean。如果是,则为true,否则为false。有许多方法可以编写该函数,这超出了本问题的范围,但您可以使用以下方法来测试该方法:

代码语言:javascript
复制
function is_imgurl_good($imgurl) {
  return false;//Check that if this returns false the previous function works
  //return true; //Comment out the first line and uncomment this one to show the reverse case.
  //put your actual code here to decide whether $imgurl is a valid image.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29951493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档