开发者

Gravatar : Is there a default image?

开发者 https://www.devze.com 2022-12-27 12:01 出处:网络
I have implemented gravatar for a portal I am building and wanted to know if there is a default image URL for gravatar? Not all people who visit the site are logged in or have email addresses, in such

I have implemented gravatar for a portal I am building and wanted to know if there is a default image URL for gravatar? Not all people who visit the site are logged in or have email addresses, in such a case, is there a开发者_JS百科 default image that can be shown (accessible via gravatar url)


To select a default image, you can use the d parameter:

http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=identicon

Or without a hash:

http://www.gravatar.com/avatar/?d=identicon

Or without the d parameter:

http://www.gravatar.com/avatar

Source: gravatar.com.


Gravatar describe the options for default images here: http://en.gravatar.com/site/implement/images/

You can select which option you want by adding the appropriate name/value pair to your url. For example, you could use "mystery person" (d=mp) which looks like this:

Gravatar : Is there a default image?

The above image was created with the url http://www.gravatar.com/avatar/?d=mp. Note that the email hash string has been omitted but normally you would include it as part of the request.

Options listed at the above link:

  • 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
  • mp: (mystery-person) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
  • identicon: a geometric pattern based on an email hash
  • monsterid: a generated 'monster' with different colors, faces, etc
  • wavatar: generated faces with differing features and backgrounds
  • retro: awesome generated, 8-bit arcade-style pixelated faces
  • robohash: a generated robot with different colors, faces, etc
  • blank: a transparent PNG image (border added to HTML below for demonstration purposes)

As you would hope, using the size option s=<pixels> also changes the size of the default image.

Gravatar : Is there a default image?

The above image was created with http://www.gravatar.com/avatar/?d=retro&s=32

Thanks to @Alireza Rezaee for the updated image types.


You can also use a custom URL for the default/fallback avatar. However, the custom URL must point to a publicly available image or else it wont show. You can learn more here and at gravatar.com.

Here is a PHP function I use to when working with gravatar images.

function myprefix_get_gravatar_url( $email, $default='mm', $size=92 )
{
    $email = md5( strtolower( trim( $email ) ) );
    $default = urlencode( $default );
    $size = (int)$size;

    $url = 'http://www.gravatar.com/avatar/';
    if ( is_ssl() )
        $url = 'https://secure.gravatar.com/avatar/';

    return $url.$email."?d=".$default."&s=".$size;
}

Example usage:

$img2x = myprefix_get_gravatar_url( 
    'spacepants@goofygoof.it',
    'https://www.google.com/images/srpr/logo11w.png',
    184
);


Not really, no. That is missing the point of the Gravatar service. It is designed so that your users can register their email address(es) and associate a gravatar image with it/them. Your site (and other sites) can then query Gravatar to give back the image which is associated with the email address in question.

If you want an image showing for users who do not even enter an email address on your website, you have two solutions that I can see:

  1. Do it in your own code. When you are dealing with a user who has no email address, you can just output a default image of your own choosing. Of course, this means it will not be done using a gravatar address and it'll be something you'll need to be serving yourself.
  2. Register an email address yourself dedicated to users who do not have/enter their own. For example, you could register something like default-gravatar@myapp.example.com and then register this with the Gravatar service and associate your chosen default image with this. Your own app code will still need to output the appropriate gravatar URL substituting this email address in place of the user's non-existant one when constructing the image URL, but it will allow you to use a gravatar URL which is something you have asked for.


For those who just want a link to a given email's image (this does not address the OP's request, but this page is prominent in web searches): From almost any POSIX shell you can generate the 256 pixel gravatar URL for "somebody@example.com" with:

echo "http://gravatar.com/avatar/$(echo -n somebody@example.com | md5sum | awk '{print $1}')?s=256"

You do need to think enough to replace somebody@example.com with the email in question. I included the "?s=256" to show how to specify the size.

I needed this to illustrate to somebody what a gravatar is.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号