Today I decided to customise my WordPress blog and I wanted add a Gravatar photo of myself on the side menu, so I decided to install PHP Code Widget and wrote the following code snippet.
If you don’t have a Gravatar account click here to create one for yourself.
How this works is that your gravatar image is associated with an email, and to access that image you need to convert an email address to a hash value using md5 function in PHP. With that hash value you need to append it to the end of the gravatar url like this:
So the code below we are converting the email address to a hash value and append it to the gravatar url and you have an option to specify the image size by providing a parameter ?s=size, size has to be an numeric value eg. ?s=200. Then we echo an <img> with the src attribute pointing to the url address:
<?php $hash = md5(strtolower(trim("firstname.lastname@example.org"))); $imgUrl = 'http://www.gravatar.com/avatar/' . $hash . '?s=200'; echo "<center><img id=\"gravatar\" style=\"border:1px solid black\" src=\"" . $imgUrl . "\" /></center>"; ?>