User login

Theming the User Profile Pictures in Drupal

In yet another installment of my latest documenting saga, we're going to cut through all the crap, and learn how to theme user profile pics in drupal.

I'm not going to take full credit for this, as most of the work was done by the good folks over at Lullabot...
http://www.lullabot.com/articles/imagecache_example_user_profile_pictures

Here's the quick step by step. It'll let your users upload pretty much any image they have for an avatar without having to resize it. It'll create a thumbnail size for display in nodes which is a link to the user profile page where a larger thumbnail is on display, all generated automatically by imagecache, and it'll also make girls think you're cool.

Resolution

OK.

STEP 1
Make sure imagecache is installed. Then install the imagecache_profiles module attached below.

STEP 2
Goto admin/user/settings. enable pictures. make the max pixel size 1600x1200 and max file size 1024. Save the page.

STEP 3
Create the imagecache presets.
Goto imagecache settings.
Create a preset called userpic_thumb with SCALE / OUTSIDE DIMENSIONS / 100 / 100 and CROP 100 100 CENTER CENTER WEIGHT:1
Create another preset called userpic_large with SCALE / OUTSIDE DIMENSIONS / 200 / 200 and CROP 200 200 CENTER CENTER WEIGHT:1

STEP 4
Paste this code into your template.php file.

function phptemplate_user_picture($account, $size = 'userpic_thumb') {
  if (variable_get('user_pictures', 0)) {
    // Display the user's photo if available
    if (preg_match('/q\=user\/\w+/', $_SERVER['REDIRECT_QUERY_STRING'])) {
        $size = 'userpic_large';
    }
     if ($account->picture && file_exists($account->picture)) {
      $picture = l(theme('imagecache', $size, $account->picture), 'user/' . $account->uid, NULL, NULL, NULL, FALSE, TRUE);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = l(theme('imagecache', $size, variable_get('user_picture_default', '')), 'user/' . $account->uid, NULL, NULL, NULL, FALSE, TRUE);
     
            
    }
    return '<div class="picture">'.$picture.'</div>';
  }
}

STEP 5
Goto your user page, upload a pic and know in your heart, you did it the Agaric Way...

Yet another quality, no-nonsense tutorial brought to you by The Quality No-Nonsense Tutorial Department at Agaric Design.

Searched words: 
theme user profile pictures drupal imagecache avatar 5

Comments

The imagecache profiles module has changed

Thanks to the good folk(s) who maintain the imagecache profiles module ( http://drupal.org/project/imagecache_profiles ) This has changed for the better. It is now even easier to do!

Just skip step 4

Then, on the user settings page you will have options to select what imagecache preset should be used wherever the user profile pics are displayed.

Thanks Dan very useful..

Thanks Dan very useful..

thanks you for tutorial,this

thanks you for tutorial,this method works with my site:www.webmasterclip.com

by the way,CAPTCHA of you site seems dosen't work properly on IE8.

drupal 6 version

How one should change for drupal 6? I tried the above code in drupal 6 template.php and it don't do anything (not giving me any error). Any thoughts?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.