User Profile avatar/picture Snippet

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($user->picture) {print theme('user_picture', $user);}?>

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

What about

funana - June 15, 2007 - 19:23

Avatar

Dzmitry Platonau - April 13, 2008 - 11:22

Here is mine version of profile avatar snippet.
I perform a check if user has administrative access or this is a user's profile page. If so, then show "noavatar" picture with the link on upload picture edit box.
If user "A" doesn't have avatar picture, then user "B" will not see "noavatar" picture when come to user's "A" profile page.

<?php
 
if ($user->picture)
  {
    print
theme("user_picture", $user);
  }
  else
  {
   
// If user has administrative access or
    // if this is a user's profile page
   
if ((user_access('administer users') ||
       
$GLOBALS['user']->uid == $user->uid))
    {
     
// Print no avatar picture
      // with the link on edit picture upload control
     
$pathedit = "$user->uid/edit#edit-picture-upload";
      print
       
'<a href="'.$pathedit.'"> '.
           
'<img class="picture"'.
           
' src="/'.
           
variable_get('user_picture', 'files/pictures/noavatar.jpg').
           
'" title="Default User Picture" /> '.
       
'</a>';
    }
  }
?>

Line 15 was wrong, this works.

husztisanyi - May 7, 2008 - 11:56

<?php
 
if ($user->picture)
  {
    print
theme("user_picture", $user);
  }
  else
  {
   
// If user has administrative access or
    // if this is a user's profile page
   
if ((user_access('administer users') ||
       
$GLOBALS['user']->uid == $user->uid))
    {
     
// Print no avatar picture
      // with the link on edit picture upload control
     
$pathedit = "?q=user/$user->uid/edit#edit-picture-upload";
      print
       
'<a href="'.$pathedit.'">'.
           
'<img class="picture"'.
           
' src="'.
           
variable_get('user_picture', 'files/avatarok/default.gif').
           
'" title="Default User Picture" /> ' . 'Upload your avatar!'.
       
'</a>'  ;
    }
  }
?>

 
 

Drupal is a registered trademark of Dries Buytaert.