If the Gravatar badges have the exact MIME types assigned by the server you are accessing, just check this. It should be image/jpegfor JPEG and image/pngfor PNG.
Otherwise...
http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header
The PNG file begins with an 8-byte signature. The hexadecimal byte values are 89 50 4E 47 0D 0A 1A 0A; decimal values are 137 80 78 71 13 10 26 10.
, ; PNG, , , . , , - ( , , ) - , . , PHP, . ( ?)
, PNG:
50 4E 47 ASCII PNG, , .