Using following snippet, you will be able to use Gravatars in your script.

Snippet Source/Credit: AppHP