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
Visitor (not verified)
Thats a big patch of code.
Fri, 2008/03/21 - 04:47Thats a big patch of code. Thanks god there is imagecache for those programming-impaired.
Khalid
Different pros/cons
Fri, 2008/03/21 - 11:26The good thing about this approach is that no new contrib modules are needed. Only core is enough.
Imagecache is more flexible, but you require additional modules to install and configure.
--
2bits -- Drupal and Backdrop CMS consulting
Visitor (not verified)
drupal 6?
Fri, 2008/03/21 - 13:03you'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
Visitor (not verified)
another way: you can do this
Mon, 2008/03/24 - 04:58another way:
you can do this with css only
live example you can see here: http://uadrom.com/
Visitor (not verified)
no luck to d6
Sat, 2008/08/02 - 07:43I 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
Visitor (not verified)
That's really nice... i
Wed, 2009/04/22 - 15:44That'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...
Visitor (not verified)
Fantastic!
Wed, 2009/10/07 - 12:41Thanks 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./