Applying imagecache presets to user profile pictures in Drupal 6

Here’s a mini tutorial on applying imagecache presets to user profile pictures. This isn’t hard to do -- a breeze compared to what one had to do in Drupal 5 -- but this write-up might still save someone a little time, I hope! While imagecache presets are easy to apply to imagefield images (this can be done from your content type settings page or views settings, imagecache presets must be applied manually* to user pictures uploaded through the user settings interface.

Out of the box, Drupal’s user profile pictures are sized down to 85x85 pixels. There’s a good chance you might want to change this – and, often, change the preset depending on the context – a blog entry, a comment, the user profile page, etc.

Imagecache Presets
Create a new preset called ‘profile_pic’ at /admin/build/imagecache/add

User Picture Settings

  • Enable user pictures at admin/user/settings
  • Set maximum dimensions and maximum file size -- generally to something reasonably large.
  • Give picture guidelines that specify the dimensions of your largest preset to prevent upscaling, "black bars" or distortion of images.

Create Your Template File

  • Copy the user-profile.tpl.php from your modules/user folder to your theme folder.
  • Let’s find the right variable to use. Add this line to your user-profile.tpl.php: <pre><?php dpm($account); ?></pre> (I like to leave this line in, and comment it out when I'm not using it during the development process. It will come in handy!)
  • Find your variable in the dpm listing. Ah, it’s $account->picture.
  • Let’s call the imagecache function by wrapping that variable in the imagecache theme.
  • This is what the generic statement looks like:
    <pre><?php print theme('imagecache', ‘my_preset’, $my_variable, $alt, $title, $attributes); ?></pre>
  • And apply that to our case:
    <pre><?php print theme('imagecache', ‘profile_pic’, $account->picture, $alt, $title, $attributes); ?></pre>
  • Test in your browser, and make sure to empty your cache to see the change. When in doubt, clear your browser cache and the website’s caches through the Devel module. Note that if you alter your imagecache preset down the line, you will need to "flush" it as well, which will regenerate your images as you access them.

Extend!

  • Now you can add more imagecache presets, and apply them to different templates. For instance, apply an imagecache preset to a user picture in their blog entry using node-blog.tpl.php:

    <pre><?php print theme('imagecache', 'profile_pic_blog', $node->picture, $alt, $title, $attributes); ?></pre>

  • If your user HASN’T added a profile picture, you can add a default picture to your server, and apply the imagecache preset to that picture, using a php statement that checks for a profile picture first, such as:
    <pre>    
    <?php if ($account->picture) {
            print
    theme('imagecache', 'profile_pic', $account->picture, $alt, $title, $attributes);
          }
          else {
            print
    theme('imagecache', 'profile_pic', default_profile_pic.png, $alt, $title, $attributes);
          }
         
    ?>

    </pre>
  • Note: If you won’t be customizing anything else with your user profile, you could instead copy your user-picture.tpl.php file from modules/user folder to your theme folder and wrap $picture in the imagecache preset instead.

*You could also install the Imagecache Profiles module , which gives you settings for applying imagecache presets for user pictures, but it will only allow you to set it site-wide, on the user page and in comments. If you needed to do anything custom beyond that, you would still need to use the method above.

Dave Hansen-Lange wrote 45 weeks 4 days ago

For those who haven't discovered devel module this chunk of code:

<?php
dpm
($account);
?>

Requries devel module to be turned on. You must also give yourself permissions to use devel. The ultra useful dpm function gives a nice print out of the variable, with JS goodness to enhance readability.

Amanda Luker wrote 45 weeks 3 days ago

Ah yes. And If you don't have Devel, you can also print your variables with something like:

<pre><?php print_r($account); ?><pre>

The <pre> tags will print it so the list is actually readable - not a big old chunk of text.

Jack Haas wrote 45 weeks 1 day ago

For extra readability, you can also wrap that in preformat html:

<?php print '<pre>'.print_r($account).'</pre>'; ?>

Aaron Winborn wrote 44 weeks 6 days ago

Or you can use

<?php drupal_set_message(print_r($account, TRUE)); ?>

so it will show up relatively prettified as a message.

Rob Russell wrote 45 weeks 2 days ago

Sounds very nice, I like the idea of adding flavour to the site with different-sized versions of the profile pic. As for finding variables XDebug is indispensable for me - just set a breakpoint where you want to investigate and you can interrogate all the variable values without changing code.

Jim wrote 45 weeks 2 days ago

Added to DrupalSightings.com

Khalid wrote 45 weeks 6 hours ago

Even easier with core only, no other module required. So less dependencies and a leaner site.

Different size user pictures (avatars) on different pages

Aaron Winborn wrote 44 weeks 6 days ago

Of course, we'll hopefully soon have ImageCache in core...

Rich wrote 45 weeks 5 hours ago

Thank you for posting this tutorial. This was quite timely as I've been researching options for manipulating user pictures i.e. Imagecache Profiles.

A couple questions:
1. Are these presets available when theming Views?
2. Do you know of a tutorial or documentation that would help me better understand the theme() function?
3. Is there a list of array variables that Drupal uses i.e. $account?

Thanks again for your tutorial.

Amanda Luker wrote 44 weeks 6 days ago

Khalid - Thanks for the link on how to do this without imagecache. Imagecache has become such a frequently used tool in my kit that I hadn't thought of trying without it!

Rich - 1. Yes, you can also access your profile pic from Views IF you have added it to the view in the view user interface: Fields -> User -> User: Picture. Then you can create a tpl.php file specific to that picture field, and then wrap it in the imagecache theme function. (Check Theme: Information on your Views settings page to determine what that template should be named.)

2. I don't have any good answers for you on this one - gets a little outside of my depth (though surely someone else here would probably know how to explain it.) There is this post on drupal.org, in which someone has the same question. Hope that is helpful!

3. Variables are available depending on the context -- like page.tpl.php has certain variables available and node.tpl.php has others. That's why I keep dpm($node) handy when theming a node, etc. It's a little trickier with profiles: you have to check dpm($account) and dpm($profile). In fact, if you don't know what object you are checking for ($node, $account, etc), you can use something like print_r(get_defined_vars());. It isn't pretty, but it will spit it all out for you.

Amanda Luker wrote 44 weeks 6 days ago

Rich, you have probably seen this, but here is the API explanation of the theme() function.

David wrote 44 weeks 4 days ago

I followed this tutorial with one small change. I copied user-picture.tpl.php into my theme folder instead of user-profile.tpl.php. This way, it affects the display of the picture, regardless of where it appears, including on the edit page.

Rich wrote 44 weeks 3 days ago

@Amanda - Thanks for your feedback. The Views tip worked great. I appreciate your suggestions on the other questions.

Hixster wrote 42 weeks 2 days ago

Hey Amanda, Got this working great when used in user_picture.tpl.php , but would like to theme nodes separately.

I'm a Drupal newb and am finding it baffling to implement this technique in views or node templates. I've tried using this in my node.tpl.php, (replacing the default print $picture)

<?php
print theme('imagecache''profile_pic' , $picture, $alt, $title, $attributes);
?>

I'm confused as when I inspect the variable output by dpm ($node), $picture contains just a path which is what I need, but the theme call just seems to dump the contents of user_picture.tpl.php into my page?

What am i doing wrong?

Hixster wrote 42 weeks 2 days ago

I finally got it working for node.tpl.php by replacing $account->$picture with $node->picture, could still use some help views-view.tpl.php or views-view-fields.tpl.php

Many thanks :-)

Amanda Luker wrote 42 weeks 2 days ago

Yay! Glad you were able to get it working in your node template. I know that figuring out where to apply it in your views template is tricky.

Did you try making a template JUST for that specific picture field? Like views-view-field--[your-view-name]--picture.tpl.php? Then do a <?php print_r($row) ?> to get the exact path to your field. In my case, I was able to use something like in my template specific to that picture field:

<?php print l(theme('imagecache', 'profile_pic', $row->users_picture, $alt, $title, $attributes); ?>

It might be different for you, though. Hope that is helpful.

Contact Us

About Amanda Luker

Amanda Luker is a web designer in Minneapolis, MN.

Advomatic on Twitter