Created
May 12, 2012 22:23
-
-
Save neonguru/2669478 to your computer and use it in GitHub Desktop.
Get Gravitar image from email - <img src="@Gravitar.profileImageUrl(email)" />
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * See https://en.gravatar.com/site/implement/images/java/ for java reference | |
| * | |
| * d=mm tells gravitar to return a mystery-man image if user is not found | |
| * More options here: https://en.gravatar.com/site/implement/images/ | |
| */ | |
| import java.security.{NoSuchAlgorithmException, MessageDigest} | |
| import java.io.UnsupportedEncodingException | |
| object Gravitar { | |
| def profileImageUrl(email: String): String = { | |
| "http://www.gravatar.com/avatar/" + md5Hex(email.trim.toLowerCase) + "?d=mm" | |
| } | |
| private def hex(array: Array[Byte]): String = { | |
| val sb = for (i <- array) yield ((i & 0xFF) | 0x100).toHexString.substring(1,3) | |
| sb.mkString | |
| } | |
| private def md5Hex(message: String): String = { | |
| try { | |
| val md = MessageDigest.getInstance("MD5") | |
| hex(md.digest(message.getBytes("CP1252"))) | |
| } | |
| catch { | |
| case e: NoSuchAlgorithmException => "" | |
| case u: UnsupportedEncodingException => "" | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment