Tech Tip: Getting Gravatar Images from Email Addresses
PRODUCT: 4D | VERSION: 13.5 | PLATFORM: Mac & Win
Published On: February 27, 2015
What is Gravatar? Gravatar is explained on the Gravatar website as:
An "avatar" is an image that represents you onlineāa little picture that appears next to your name when you interact with websites.
A Gravatar is a Globally Recognized Avatar. You upload it and create your profile just once, and then when you participate in any Gravatar-enabled site, your Gravatar image will automatically follow you there.
Gravatar is a free service for site owners, developers, and users. It is automatically included in every WordPress.com account and is run and supported by Automattic.
A Gravatar is a Globally Recognized Avatar. You upload it and create your profile just once, and then when you participate in any Gravatar-enabled site, your Gravatar image will automatically follow you there.
Gravatar is a free service for site owners, developers, and users. It is automatically included in every WordPress.com account and is run and supported by Automattic.
The Gravatar explanation above is directed more towards end-users. However as a developer you may be wondering, how can i use this within my own application?
The Gravatar API is documented online but it is rather easy if all you want to do is simply grab the image associated with a specific email address.
The following method can be used for obtaining a Gravatar image from the web:
// GRAVATAR_GET_IMAGE //* C_TEXT($1) := Email address to show gravatar for //* C_TEXT($2) := Rating of Gravatar(G, PG, R, X) //* C_BOOLEAN($3) := Secure/SSL. True to use https, False or omitted to not //* C_PICTURE($0) := picture var for image found */ C_TEXT($1;$email;$2;$rating;$md5;$grav_base_url;$grav_image_url) C_BOOLEAN($3;$secure) C_PICTURE($0;$image_out) If (Count parameters>0) $email:=Replace string(Lowercase($1);char(SP ASCII code);"") If (Count parameters>1) $rating:=$2 // pass the rating specified (valid ratings are; G, PG, R, X) Else $rating:="G" // param 2 doesnt exist, set the rating to G rated images End if If (Count parameters>2) $secure:=$3 Else $secure:=False End if // get md5 hash of email $md5:=Generate digest($email;MD5 digest) // build gravatar base url If ($secure) $grav_base_url:="https://secure.gravatar.com" Else $grav_base_url:="http://www.gravatar.com" End if // build gravatar image url $grav_image_url:=$grav_base_url+"/avatar/"+$md5+"?r="+$rating // download image to 4D picture var HTTP Get($grav_image_url;$image_out) // return image $0:=$image_out End if |
If the method above is saved as GRAVATAR_GET_IMAGE then it can be used in the following manner:
C_PICTURE(test) test:=GRAVATAR_GET_IMAGE ("tpenner@4d.com";"G";True) |
If a Gravatar image is associated with the email address that image will be returned. Otherwise, if no image is associated with the email address the default Gravatar icon is return.
Further Customization
The URL built can be further customized based on numerous variables found here.
For example;
- You can specify a default image to use if no image is found to be associated with the specified email address.
- You can instruct the server to return a unique icon based on the email address if no image is found
- You can specify a size in pixels for the image (the image is always square)