0

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";
   }
}
ByteHamster
  • 4,884
  • 9
  • 38
  • 53

1 Answers1

0

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.
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • With this no pictures appear at all. Not sure what to do with the function is_imgurl_good I'm not that advanced coder You see. – user4843525 Apr 29 '15 at 21:24
  • If you implement the above and have `is_imgurl_good` return `true` instead of `false`, does your image show? – Nathaniel Ford Apr 29 '15 at 21:40
  • Then the user pics show but the gravatar for those who didn't set up profile pic does not. – user4843525 Apr 29 '15 at 23:04
  • Nor will it until you decide whether the user pic is good or bad. Check this question: http://stackoverflow.com/questions/1363925/check-whether-image-exists-on-remote-url Implementing that in `is_imagurl_good` will solve your problem. – Nathaniel Ford Apr 29 '15 at 23:09
  • I tried this: `function is_imgurl_good($imgurl) { if (@getimagesize($imgurl)) return true;//Check that if this returns false the previous function works //return false; //Comment out the first line and uncomment this one to show the reverse case. }` but it doesn't works – user4843525 Apr 30 '15 at 00:07