Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created October 10, 2012 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhobbs/3868638 to your computer and use it in GitHub Desktop.
Save jmhobbs/3868638 to your computer and use it in GitHub Desktop.
Example of consistently selecting a default avatar from a set of available.
<?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