A while back, we need to do display different sized user pictures (avatars) in Drupal 5.x on a client site. They wanted the user picture on the user profile page to be 100x100 pixels, but be displayed only as 65x65 on blog comments.

This is fairly easily achieved by creating this function in template.php. This function creates 65x65 size images from the original 100x100, and takes an argument of size so either can be used in different places.

function phptemplate_user_picture($account, $size = '65x65') {
  if (!variable_get('user_pictures', 0))  {
    return '';
  }

  // Default to a certain size
  if (arg(0) == 'user' && is_numeric(arg(1))) {
    $size = '100x100';
  }

  if ($account->picture && file_exists($account->picture)) {
    switch($size) {
      case '100x100':
        $maxsize_icon = array('w'=> 100, 'h'=> 100);
        $info = image_get_info($account->picture);
        if ($info['height'] picture) . '/picture-' 
         . $account->uid . '.' . $info['extension'];
        if (!file_exists($newpicture) || 
          (filectime($newpicture) picture))) {
          image_scale($account->picture, $newpicture, $maxsize_icon['w'],
            $maxsize_icon['h']);
        }
        $picture = file_create_url($newpicture);
        break;

      case '65x65':
        $maxsize_tile = array('w'=> 65, 'h'=> 65);
        $info = image_get_info($account->picture);
        $newpicture = dirname($account->picture) . '/picture-' 
          . $account->uid . '-small' . '.' . $info['extension'];
        if (!file_exists($newpicture) || 
          (filectime($newpicture) picture))) {
          image_scale($account->picture, $newpicture, $newpicture, 
            $maxsize_tile['w'], $maxsize_tile['h']);
        }
        $picture = file_create_url($newpicture);
        break;

      default:
        $picture = file_create_url($account->picture);
        break;
    }
  }
  else {
    $picture = variable_get('user_picture_default', '');
  }

  if (isset($picture)) {
    $alt = t('@user\'s picture', 
      array('@user' => $account->name ? $account->name : 
        variable_get('anonymous', 'Anonymous')));
    $picture = theme('image', $picture, $alt, $alt, '', false);
    if (!empty($account->uid) && user_access('access user profiles')) {
      $picture = l($picture, "user/$account->uid", array('html' => TRUE));
    }

    return $picture;
  }
}

Once you have that in the theme, you can select the small size like this:

<?php if ($picture) { print theme('user_picture', $node, '65x65'); }?> 

Or the normal size like this:

<div class="picture"><?php print theme('user_picture', $user, '100x100'); ?></div>

Now, all you need to do is select the size you want in the various places you need to display the user picture.

Comments

Fri, 2008/03/21 - 13:03

you're doing some great stuff, this is exactly what i was looking for and have been requesting info in drupal forums... but i'm using drupal 6 now and was wondering if this would apply to drupal 6 and if no what needs to change?

thanks

Mon, 2008/03/24 - 04:58

another way:
you can do this with css only

.comment .picture img {
  max-width: 24px !important;
  max-height: 24px !important;
}
/* for IE6 */
* html .comment .picture img {
  width: 24px !important;
  height: 24px !important;
}
/* end for IE6 */

live example you can see here: http://uadrom.com/

Sat, 2008/08/02 - 07:43

I have been waiting for drupal 6 solution for quite a long long time to this since most of my sites hosts only allows 32M memory_limit which is ridiculously too small for imagecache. I have tried this with d6, and I saw picture-small-1.jpg was produced but it didn't seem to appear in user profile page. Any help? Thanks for throwing the lights

Wed, 2009/04/22 - 15:44

That's really nice...
i tried, but it outputs a link of the html code of the image (<img ...) instead of the image

what's happening?
i use drupal 6.10
imagecache
imagecache profile

and the picture is inside a view...

Wed, 2009/10/07 - 12:41

Thanks for this one K - I can't tell you how I have always scratched my head when dealing with older sites that use the conventional method for user profiles with d5 in regards to theming user lists!

This is an ace method.

q./

Is your Drupal or Backdrop CMS site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal or Backdrop CMS Performance Optimization and Tuning Consulting