How to build a url of a GRAVATAR image from a given email
Asked Answered
T

6

11

There is a simple way with php, a simple script or URL manipulation to build a URL for the gravatar image corresponding to an email?

Ex. http://gravatar.com/avatars/[email protected] and this return a jpeg or png image.

If there is no simple way like the example, what is the easiest way you know to resolve a url of the gravatar corresponding to an email?. Thanks

Tannie answered 27/4, 2010 at 5:9 Comment(0)
D
8

You can find a sample script with PHP code on their implementation site: http://en.gravatar.com/site/implement/php

Debbee answered 27/4, 2010 at 5:20 Comment(0)
A
10

Use this:

$userMail = whatever_to_get_the_email;

$imageWidth = '150'; //The image size

$imgUrl = 'http://www.gravatar.com/avatar/'.md5($userMail).'?size='.$imageWidth;

(Fixed the $imgUrl)

Aphonic answered 20/12, 2011 at 19:40 Comment(1)
There's an error in the url, before md5 of user email there should be no .Schaaf
D
8

You can find a sample script with PHP code on their implementation site: http://en.gravatar.com/site/implement/php

Debbee answered 27/4, 2010 at 5:20 Comment(0)
A
6

The root script is at http://www.gravatar.com/avatar/ The next part of the URL is the hexadecimal MD5 hash of the requested user's lowercased email address with all whitespace trimmed. You may add the proper file extension, but it's optional.

The complete API is here http://en.gravatar.com/site/implement/

Appurtenance answered 27/4, 2010 at 5:17 Comment(0)
B
1

Though @dipi-evil's solution works fine, I wasn't getting larger image with it. Here's how I got it working properly.

$userMail = 'johndoe@example';

$imageWidth = '600'; //The image size

$imgUrl = 'https://secure.gravatar.com/avatar/'.md5($userMail).'?size='.$imageWidth;
Barbaresi answered 9/12, 2016 at 15:42 Comment(0)
F
1

You can just see this simple function of Gravatar which can:

  1. Check if the email has any gravatar or not.
  2. Return the gravatar image for that email.

    <?php
    
    class GravatarHelper
    {
    
      /**
      * validate_gravatar
      *
      * Check if the email has any gravatar image or not
      *
      * @param  string $email Email of the User
      * @return boolean true, if there is an image. false otherwise
      */
      public static function validate_gravatar($email) {
        $hash = md5($email);
        $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
        $headers = @get_headers($uri);
        if (!preg_match("|200|", $headers[0])) {
          $has_valid_avatar = FALSE;
        } else {
          $has_valid_avatar = TRUE;
        }
        return $has_valid_avatar;
      }
    
      /**
      * gravatar_image
      *
      *  Get the Gravatar Image From An Email address
      *
      * @param  string $email User Email
      * @param  integer $size  size of image
      * @param  string $d     type of image if not gravatar image
      * @return string        gravatar image URL
      */
      public static function gravatar_image($email, $size=0, $d="") {
        $hash = md5($email);
        $image_url = 'http://www.gravatar.com/avatar/' . $hash. '?s='.$size.'&d='.$d;
        return $image_url;
      }
    
    }
    

You can use then like:

if (GravatarHelper::validate_gravatar($email)) {
   echo GravatarHelper::gravatar_image($email, 200, "identicon");
}
Fransiscafransisco answered 1/7, 2018 at 10:25 Comment(0)
S
0

You can use this code to get an email's avatar set on Gravatar.com or generate a default avatar if no avatar found for that email.

You just need to pass an email as a parameter and call this function anywhere in your project.

public function  get_avatar($email){

    $url = 'https://www.gravatar.com/avatar/'; // The gravatar's API url
    $url .= md5( strtolower( trim( $email ) ) ); // Hash the user's email
    $url .='.png?s=300'; // Get a custom image size

        //Extract the image if is set on Gravatar

    if ( isset($img) ) {
        foreach ( isset($atts) as $key => $val )
            $url .= ' ' . $key . '="' . $val . '"';
    }

    return $url; // Return the avatar's url or the default avatar if no image found.

}
Stump answered 7/9, 2020 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.