douxuan1284 2015-04-29 18:18
浏览 21

获取默认用户图片drupal 6

I have a Facebook-style chat on my drupal website, however the script was lacking user picture integration. I managed to get user pictures, but if the user didn't set picture and is using a default picture I'm getting a broken image and a 404 not found in the logs. I've tried to fetch gravatar for users without a picture set, but couldn't make it work.

/**
 * 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";
   }
}
  • 写回答

1条回答 默认 最新

  • dongshendie8849 2015-04-29 20:31
    关注

    Your issue is that you return your image immediately, regardless of whether it's there:

     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";
       }
     }
    

    What you need to do is the following:

     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;
     }
    

    In the above, you will have to write a function, is_imgurl_good that returns a boolean indicating whether the image is good. true if it is and false if not. There are many ways to go about writing that function which is beyond the scope of this question, but you can test the approach with this:

    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.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分