I got this cool bit of code from this post: http://drupal.org/node/402142

  $pict =  $content_profile->get_variable('profile', 'field_userpictures');
  $title =  $content_profile->get_variable('profile', 'title');
  print theme('imagecache','Thumbnail', "user_pictures/".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title); 

Throwing it in a custom user-picture.tpl.php allows for me to use my imagefield avatar to replace the default user pic throughout the site in my theme. It works great, but now I need a little extra functionality.

With this method, there is no longer a default image supplied when the user hasn't uploaded an avatar. I only get 'Username's Profile' where the image would be because it just returns the profile title. Though I have attached a default to my field_avatar using the imagefield settings, it only shows in the user profile (I am using the Content Profile module) because the script isn't set up to grab any default.

I thought maybe I could modify the code a little, but I know virtually nothing about PHP (yet). I THINK something like the below code would work, but I don't have the knowledge to get it right. I don't know how to set it up for the filepath (since it depends on the UID), or for that mattter, even whether file_exists is what I should be using.

  $pict =  $content_profile->get_variable('profile', 'field_avatar');
  $title =  $content_profile->get_variable('profile', 'title');
  $filename = WHAT GOES HERE?!
  
  if (file_exists($filename))  {
    print theme('imagecache','thumbnail', "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
  }
  else {
	print '<img src="/sites/default/files/default-user.png" alt="" />';
  }

If anyone can tell me how to set $filename to the right path, or a better way to check whether or not the avatar file exists, I would be very grateful. I am trying to learn PHP now, but I would prefer not to have to wait until I understand it all because I don't know how long it will take me to grasp it.

Comments

alan d.’s picture

This looks good without actually running it, the account should have the picture file path details $account->picture


Alan Davison
Geekish’s picture

I gave that a try:

  $pict =  $content_profile->get_variable('profile', 'field_avatar');
  $title =  $content_profile->get_variable('profile', 'title');
  $filename = $account->picture;
  
  if (file_exists($filename))  {
    print theme('imagecache','thumbnail', "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
  }
  else {
    print '<img src="/sites/default/files/default-user.png" alt="" />';
  }

It actually overrode ALL the avatars with the default. Hmmm.

jamestombs’s picture

Filename would be this:


"/user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename']

Trying doing a var_dump to make sure.

James T
Action Medical Research - www.action.org.uk

Geekish’s picture

I put this in the file path to check it out. It ends up overriding all avatars with the default image, the same as when I try a non-existent path. Seems like that would mean the path isn't correct, but I tried the var_dump out and it returned this for the filepath:

["filepath"]=> string(63) "sites/default/files/user_pictures/user3/myavatar.jpg"

That's pretty much what I expected, since I set the field_avatar path to user_pictures/user[uid], so I'm confused as to why this did not work. If "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'] works to show the custom avatars, then why wouldn't that same path work to check if there actually is an avatar file or not?

Just to be thourough, I tried with/without a preceding slash, using the path "sites/default/files/user_pictures/user" and even putting the whole absolute path. It's the same all around.

jamestombs’s picture

Set the code to do a var_dump of $pict and get the output for a user with an avatar and one without.

James T
Action Medical Research - www.action.org.uk

Geekish’s picture

Okay, I put that in the user-picture.tpl.php file. I get this for a user with an avatar:

array(1) { [0]=> array(10) { ["fid"]=> string(1) "8" ["list"]=> string(1) "1" ["data"]=> array(3) { ["description"]=> string(0) "" ["alt"]=> string(0) "" ["title"]=> string(0) "" } ["uid"]=> string(1) "1" ["filename"]=> string(21) "avatar.jpg" ["filepath"]=> string(63) "sites/default/files/user_pictures/user1/avatar_0.jpg" ["filemime"]=> string(10) "image/jpeg" ["filesize"]=> string(6) "108775" ["status"]=> string(1) "1" ["timestamp"]=> string(10) "1239647606" } }

And this for one without one:

array(1) { [0]=> NULL }

jamestombs’s picture

For the code you could use:

if (is_array($pict[0])) {
... // print user avatar
}
else {
... // print default
}

James T
Action Medical Research - www.action.org.uk

Geekish’s picture

Great! That did the trick nicely. Thanks for your help!

(Now I'm off to learn some PHP!) :-)

jamestombs’s picture

No prob.

James T
Action Medical Research - www.action.org.uk

gbrussel’s picture

Not tested though:

<?php
  $pict =  $content_profile->get_variable('profile', 'field_avatar');
  $title =  $content_profile->get_variable('profile', 'title');
 
  if ($pict !== '')  {
    print theme('imagecache','thumbnail', "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
  }
  else {
    print '<img src="/sites/default/files/default-user.png" alt="" />';
  }
?>

Essentially, if the $pict variable has something in it (i.e. it has the image), then print the image, else print default.

Geekish’s picture

I tried out this code too. Sounds like something that would have worked, but it still served up only the existing avatars and left the others blank. Strange. Until the user actually uploads an avatar, there is not even a folder, since I set the field_avatar path to generate one at user_pictures/user[uid]. So I'm not sure how the script thinks there's something to find for the users with no avatar.

andrenoronha’s picture

hello....
the new imagefield 3.0 got a place to set a default image, it works for me...

i tried all the codes in this page but i couldnt get the file name...
it stops in the user id directory... like: "/5/" so no image is shown

do i have to change "profile" to my profile type name? cause it is "perfil" (portuguese)

and is $pict[0]['filename'] really correct?

I need this cause i want 2 different sizes of the avatar image. one bigger in a block showing the user, and a small one in the blocks showing users.
i'm in doubt between the default user pic or the imagefield one.
the user pic is good cause it comes with the correct link to the user page
but i couldnt make it work with imagecache in a views block to show the bigger version of it.

any help?

Geekish’s picture

I don't know how much use I can be (I'm new to PHP) but I'll try to help a bit... This is the code copied from my site theme's user-picture.tpl.php:

$pict = $content_profile->get_variable('profile', 'field_avatar');
$title = $content_profile->get_variable('profile', 'title');

if (is_array($pict[0])) {
print theme('imagecache','thumbnail', "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
}
else {
print theme('imagecache','thumbnail', "sites/default/files/default-user.png", $title, $title);
}

You're right, you do have to change 'profile' to the machine-readable name of your content type. And 'field_avatar' should be the name of your imagefield, while 'thumbnail' should be your ImageCache preset name.

You may already have done this part, but I forgot to mention that I used the Token module when I set up my ImageField field_avatar path. In Manage Fields>Avatar, under the Path settings, I set the path as: user_pictures/user[uid] so when user 4 (for example) uploads an avatar with my imagefield it's stored in sites/default/files/user_pictures/user4/user-avatar.jpg

From what I understand, that's why my path has to be "user_pictures/user".check_plain($account->uid)."/".$pict[0]['filename'] so it can check the user ID on the content and fill in the right path to that user's image.

I didn't change anything with the $pict[0]['filename'] part to get it working.

I don't know if you're using the Content Profile module like I am, but I had to edit the file content_profile.module like in the original post before my field would work in my user-picture template.

andrenoronha’s picture

wow!! thank you so much! it helped me a lot! i got there...
that was my mistake:

i didnt edit the content_profile.module file, i just added that code to my template.php file, but it didnt solve the message: "Fatal error: Call to a member function get_variable() on a non-object in...."
so i used this code i saw in another post inside user-picture.tpl.php:

$content_profile = new content_profile_theme_variables($uid);

it solve the message, but in some way didnt let my cck filed user images files to be reconized...

and now i saw that in fact i dont really need this anymore...i arrange that user link thing... i just realized that i could create a uid field excluded from the view and get it in the pic url with: [uid]
so i dont need core user image system anymore!

thank you all!

Geekish’s picture

I'm glad to hear you were able to work it out. I also like not needing to rely on the core user images.

andrenoronha’s picture

hey, it's me again...
now i have another issue...
how can i get the imagefield user picture in the comment.tpl.php?
i'm using this same hack that i used before, but it's not showing the picture like before...
does $pict[0]['filename'] change in the comment template?

thnks

rickh’s picture

Geekish

If I could kiss you right now, i would. You're a legend mate, thanks for the help.

MAds’s picture

This works perfectly in the sense that if the user uploads a photo, I have no problems. Also if there is a deleted user, the default image will work. The problem is that for current and new users, I cant get the default image to work in the comments, forum, etc. The default will work Everywhere Else (avatar, whos online, etc) just not in this particular area.

Ive discovered that the problem is that Just For Comments, Forum, etc, the imagecache is using a completely different path, that is unique to every user. This is also a path that is different from the uploaded avatar path.
Any insights on what can be overriding my theme's user-picture.tpl.php would be very helpful!

Thanks!

darrellhq’s picture

Nice, I could use a module like this. I hope that you continue to develop on it.