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>
halfer
  • 19,824
  • 17
  • 99
  • 186
PHPLOVER
  • 7,047
  • 18
  • 37
  • 54

4 Answers4

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.

tkrajcar
  • 1,702
  • 1
  • 17
  • 34
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

Jeremy S.
  • 6,423
  • 13
  • 48
  • 67
  • Don't use fopen for remote sites. Use [CURL](http://php.net/curl) because it is more feature rich. – Kevin Peno Apr 13 '11 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 Apr 13 '11 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
)
wlf
  • 851
  • 1
  • 9
  • 15
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);
       }
Softmixt
  • 1,658
  • 20
  • 20