Misc PHP

How to fetch Gravatar image using PHP

    In this tutorial we will see how to fetch Gravatar image using PHP ? Gravatar uses MD5 algorithm to generate unique hash value of users email address. MD5 is a widely used cryptographic hash function which generates 128-bit (16-byte) hash value which is nothing but 32-digit hexadecimal numbers.
Lets start with tutorial fetch Gravatar image using PHPFor this we will need users email address.
1) Suppose email address is “dev.xxxxx@gmail.com” then
– Make sure there are no white spaces around it, better to use php trim function.
– Convert email address to small case because MD5 is case sensitive and generates two different hash values for same email address with different case.Ex : For email address amit@gmail.com it will create hash key like “4137f5d72b8cf2ee0cad3bf00e165d5f” and for AMIT@GMAIL.COM it would create “ead586effc62adea6ae8003e717d6d22”.

2) Use php MD5 function and pass email address as a argument to that function
md5( “developer.amitpatil@gmail.com” );
it will return hash value of that email address.
now simply append generated hash key to gravatar API url. http://www.gravatar.com/avatar/Generated-HASH-key

Ex : http://www.gravatar.com/avatar/c2243b8fa53bc62c8ffd029978799ddb
Copy this url and paste in browsers address bar and you will see gravatar image.

To embed this in your web page just wrap this link with <img> tag. which will print gravatar image on your web page. like <img src=”http://www.gravatar.com/avatar/c2243b8fa53bc62c8ffd029978799ddb”> will produce image like

fetch Gravatar image using PHP

By default it displays 80X80 px image, you can customize its size by passing s= or size= parameter to the url like
<img src=”http://www.gravatar.com/avatar/c2243b8fa53bc62c8ffd029978799ddb?s=30“> will produce image like below

Lets see more examples

 
 
 
 
 
 

3 Comments

Leave a Comment

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shares

Let your friends know what are you reading

Share this post with your friends!