1

I basically retrieve a user's gravatar image and uses that for their profiles on my site. If a user doesn't have a gravatar account or email is not assigned to their gravatar account a default image is shown instead from gravatar.

My question is, if for example gravatars website went down and I could not retrieve the user's gravatar image, is there anyway in php I can check to see if gravatar is returning an image and if not display a some text or something?

Obviously I can do a simple if statement; but checking in PHP if gravatar is returning an image is what I am not sure on.

If I cannot check if a image is being returned from Gravatar, checking if the link is online (accessible) would be fine I guess.

UPDATE:

I forgot to mention this is how i retrieve the image from gravatar:

function get_gravatar($email, $s = 50, $r = 'pg')
{
$defaultimage='mm';
$url='http://www.gravatar.com/avatar/';
$url.=md5(strtolower(trim($email)));
$url.="?s=$s&d=$defaultimage&r=$r";
return $url;
}

Then to display I do:

  <a href="http://www.gravatar.com" target="_blank">
  <img class="profileimgright profileimgframe" alt="Profile Image" src="<?php echo get_gravatar($_SESSION['email'], 180, 'pg') ?>" />
  </a>
4
  • 2
    How are you retrieving the gravatar? Commented Apr 13, 2011 at 16:23
  • How are retrieving the gravatar icon? Are you just building a URL string? Commented Apr 13, 2011 at 16:25
  • No information answer is to set a timeout, check the HTTP Status header, and/or check the returned Content-Type header is of type image/*. I would obviously use CURL as well
    – Kevin Peno
    Commented Apr 13, 2011 at 16:27
  • Hi, Sorry i forgot to add that silly me, just added it to my main post. Thanks phplover
    – PHPLOVER
    Commented Apr 13, 2011 at 16:29

4 Answers 4

4

While this doesn't answer your precise question, I'm guessing you actually probably don't need to worry about whether or not you're getting an image from them, because you can tell Gravatar to use a specific image as the 'default' if they don't have one. That way, your code just always calls Gravatar and they will determine whether to show the user's real image or your specified default.

On http://en.gravatar.com/site/implement/images/, see the 'Default Image' section.

1

the simplest way to go would be to check if a file exists on an external url would be via fopen

<?php
   function fileExists($path){
     $exists = @fopen($path,"r");
     if($exists){
        fclose($datei);
        return true;
     }
     return false;
  }
?>

then you could call it like this

<?  
   $url = get_gravatar($_SESSION['email'], 180, 'pg');
   $exists = fileExists($url);
?>

Regards

2
  • Don't use fopen for remote sites. Use CURL because it is more feature rich.
    – Kevin Peno
    Commented Apr 13, 2011 at 16:38
  • @ Jeremy S. Thanks for this. @Kevin Peno. Never used curl and don't need anything special but thanks for your suggestion and everyone elses. Thanks!
    – PHPLOVER
    Commented Apr 13, 2011 at 17:57
0

you can check whether a url is returning an image with getimagesize: http://php.net/manual/en/function.getimagesize.php

It will return false if it's not.

Examples:

$asd = getimagesize('/etc/passwd');//(bool) false
$asd = getimagesize('asd"asd');//(bool) false
$asd = getimagesize('localhost');//(bool) false
$asd = getimagesize("http://google.com/' OR ''='");//(bool) false
$asd = getimagesize('localhost.com');//(bool) false
$asd = getimagesize('https://www.google.co.uk/images/loading.gif');//array(7) (
  [0] => (int) 80
  [1] => (int) 80
  [2] => (int) 1
  [3] => (string) width="80" height="80"
  [bits] => (int) 7
  [channels] => (int) 3
  [mime] => (string) image/gif
)
0
     function gravatar($email, $size = 80, $default_image = 'default', $rating = 'g', $class = '')
     {
                $gravatar_url = 'http://www.gravatar.com/avatar/';
                $gravatar_url .= md5(strtolower(trim($email)));
                $gravatar_url .= "?s=$size&d=$default_image&r=$rating";

                if ($class !== '')
                {
                    $class = 'class="'.$class.'"';
                }

                $gravemail = md5( strtolower( trim( $email ) ) );
                $gravcheck = "http://www.gravatar.com/avatar/".$gravemail."?d=404";
                $response = get_headers($gravcheck);
                if ($response[0] != "HTTP/1.0 404 Not Found")
                {
                    return '<img src='.$gravatar_url.' alt="gravatar" width="'.$size.'" height="'.$size.'" '.$class.'>';
                }
                else
                {
                    return false;
                }

        }

       if(gravatar($youremail))
       {
         echo gravatar($youremail);
       }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.