Created
October 10, 2012 21:42
-
-
Save jmhobbs/3868638 to your computer and use it in GitHub Desktop.
Example of consistently selecting a default avatar from a set of available.
This file contains 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
<?php | |
// This is how many different "random" avatars you have. | |
$default_avatar_count = 20; | |
foreach( $comments as $comment ) { | |
// Normalize the email address so user input differences doesn't change the avatar. | |
// This is technically against RFC 2821, but in practice it is ok. | |
$normalized_email_address = trim(strtolower($comment->comment_author_email)); | |
// Take the normalized email, hash it, take the most significant bits (a full sha1 would be too large an integer), | |
// convert to an integer, then modulo to our count and shift to avoid 0 indexing. | |
$default_avatar_id = ( hexdec(substr(sha1($normalized_email_address), 0, 7)) % $default_avatar_count ) + 1; | |
// Your default avatars should be named 1.jpg, 2.jpg ... | |
echo get_avatar( $comment, 32, "/avatars/{$default_avatar_id}.jpg" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment