Male and Female Default (Gender Based) Avatar Images for Drupal User Profiles

I was sitting here thinking tonight that there must be a strong use case for male and female default avatars since there are several sites out there with users that with to remain "anonymous" but give enough information to determine their gender. I mulled over trying to make a module out of this but I don't think it would be advantageous enough to fix this customization to only 2 options and some how tying that to a field in the profile. So I decided to blog about how you could program this yourself.

I'm creating a site as my DBA, Associate Innovations, that deals with households, and I thought it would be neat to have Male and Female default avatars to give it a little extra flare. I picked up the images from, which if you look I made a comment on how surprised I was to see these quality images supplied for free. So lets see how we can implement those.

Lets start off with a custom module. We know from looking at the user module and template_preprocess_user_picture we can set the Drupal variable user_picture_default and get what we want... in addition, we should be able to call this preprocess function and also get what we want. It has been my experience though that some modules like Display Suite can forgo the preprocess functionality, but we will ignore that for now.

So lets make a preprocess function that will do this for us...

function genderpicture_preprocess_user_picture(&$variables) {
  $variables['user_picture'] = '';
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    if (!empty($account->picture)) {
      // @TODO: Ideally this function would only be passed file objects, but
      // since there's a lot of legacy code that JOINs the {users} table to
      // {node} or {comments} and passes the results into this function if we
      // a numeric value in the picture field we'll assume it's a file id
      // and load it for them. Once we've got user_load_multiple() and
      // comment_load_multiple() functions the user module will be able to load
      // the picture files in mass during the object's load process.
      if (is_numeric($account->picture)) {
        $account->picture = file_load($account->picture);
      if (!empty($account->picture->uri)) {
        $filepath = $account->picture->uri;
    // We could make this a configurable field with configurable options stating what M or F should be.
    elseif (!empty($account->field_user_male_or_female)) {
      $gender = reset(field_get_items('user', $account, 'field_user_male_or_female'));
      switch ($gender) {
        case 'm':
          $filepath = variable_get('user_picture_default_male', '');
        case 'f':
          $filepath = variable_get('user_picture_default_female', '');
          $filepath = variable_get('user_picture_default', '');
    elseif (variable_get('user_picture_default', '')) {
       $filepath = variable_get('user_picture_default', '');
    if (isset($filepath)) {
      $alt = t("@user's picture", array('@user' => format_username($account)));
     // If the image does not have a valid Drupal scheme (for eg. HTTP),
      // don't load image styles.
      if (module_exists('image') && file_valid_uri($filepath) && $style = variable_get('user_picture_style', ''))     {
        $variables['user_picture'] = theme('image_style', array('style_name' => $style, 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
      else {
        $variables['user_picture'] = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
      if (!empty($account->uid) && user_access('access user profiles')) {
        $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
        $variables['user_picture'] = l($variables['user_picture'], "user/$account->uid", $attributes);

Notice you will need to make sure your field is named with the machine name of "field_user_male_or_female" and that it's values are m, f. If you add something like "?" in there, it will default to the normal default picture.

Next, you could add to the administration interface, but I love to rely on good ole drush when I'm doing things on the fly so you could do something like

drush vset user_picture_default_male public://male.png drush vset user_picture_default_male public://female.png

Tada... Male and Female default images!