User Profile avatar/picture Snippet

Last modified: August 23, 2009 - 15:16

PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet checks to see if the user has a picture/avatar uploaded and displays it if they have.

Dependencies: No extra modules required but picture support must be enabled to allow users to upload their avatars/pictures:

1) admin/themes/settings/*yourtheme*
Enable 'User pictures in posts' and 'User pictures in comments'

2) admin/settings/user
'Enable' Picture support. You can also set size of avatar.

Usage

  • For use in your user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the code into your user_profile.tpl.php file
  • Tested and works with Drupal 4.5 and 4.6
  • Change the div class names or the prefix text to suit.

<?php  if ($account->picture) { print theme('user_picture', $account); } ?>

NOTES: Thanks to leafish_paul for improving this snippet.

Working in Drupal 5 + no picture support

ifthatsthewayitis - April 6, 2007 - 11:35

<?php  if($user->picture) {print theme('user_picture', $user);}
else {
print
'<img src="/path/to/nopicture.gif" alt="This user hasn't posted a picture yet or whatelse" />';
}
?>

This version works in Drupal 5 but I added an else to display a no-picture picture so the space is not left empty on profiles without a user picture.

code for This user not posted picture is not working

gurukripa - May 8, 2007 - 17:16

hi..
i am gettting a parse error..is there something wrong with the code for 5.1
pls help.
thanks

try this

Dublin Drupaller - May 9, 2007 - 11:32

I think there's an extra apostrophe in there..

<?php  if($user->picture) {print theme('user_picture', $user);}
else {
print
'<img src="/path/to/nopicture.gif" alt="This user hasn\'t posted a picture yet or whatelse" />';
}
?>

Note how you put a backslash before the apostrophe in hasn't

hope that helps

Dub

Also, I think that it can be

baronmunchowsen - May 30, 2007 - 20:47

Also, I think that it can be written like this too:

<?php
 
if ($user->picture) {
    print
theme('user_picture', $user);
  }
  else {
    print
'<img src="/'.variable_get('user_picture_default', 'path/to/default.gif').'" alt="Default User Picture" />';
  }
?>

or for use in various node template files:

<?php
 
if ($node->picture) {
    print
theme('image', $node->picture);
  }
  else {
    print
'<img src="/'.variable_get('user_picture_default', 'path/to/default.gif').'" alt="Default User Picture" />';
  }
?>

the variable_get means that you can still us the administrative interface to update the default user picture down the road, rather than have to go back and edit all the files where you have this code.

A mix works for me

Eddie123 - March 26, 2008 - 20:13

<?php
global $user;
if (
$user->uid) {print theme("user_picture",$profileuser);}
  else {
    print
'<img src="/'.variable_get('user_picture_default', '/noavatar.png').'" alt="Default User Picture" />';
  }
?>

Put this code to your "node-profil" template.

www.yoci.de

Best Practice

kingandy - June 24, 2009 - 10:00

It's generally a good idea to run any user-displayed text - even image alt tags - through Drupal's t() function, in case your theme gets used on a site which uses the various foreign language modules. No biggie, just something like this:

<?php
... .'" alt="'. t('Default user picture') .'" />'...
?>

--Andy
Developing Drupal websites for Livelink New Media

Block pictures in pages and not in blogs ?

-Tiziano- - November 16, 2008 - 22:31

Is it possible to show avatars only in blogs and comments but not in pages/stories ?

How to do it please ?
Tiziano

What about

funana - June 15, 2007 - 19:23

D6?

stevekerouac - May 19, 2009 - 18:56

What would be the equivalent in Drupal 6?

it works!

animeonx - July 15, 2009 - 20:30

<?php
 
if($user->picture) {
    print
theme('user_picture', $user);
}
else { print
'<img src="/images/avatar.gif"/>';}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.