Hi all,

i could get the user picture by the following code

  $user = user_load($uid);
  print theme('user_picture', array('account' =>$user));

this will render the default user picture size. is there anyway i can apply my own image style(imagecache) on it?

Thanks.

Regards,
Kit

Comments

findmashmind’s picture

Try this code,
print theme('user_picture', array('account' =>$user,'style_name' => 'content_highlighter'));

Replace "content_highlighter" with your image style name.

ykyuen’s picture

thanks. but this does not work.

tecjam’s picture

You need to get the uri of the picture.
That should be:

$user->picture[0]['uri']

Then use the image_style_url (http://api.drupal.org/api/drupal/modules--image--image.module/function/i...) to get the full path to the image to a specific imagecache preset.

You could define the thumbnail style like this maybe:

// name of the imagecache preset found at /admin/config/media/image-styles
$THUMBNAIL_STYLE = 'thumbnail'; 


// now get the full image url from the uri and the style
$default_thumbnail = image_style_url($THUMBNAIL_STYLE, $user->picture[0]['uri']);

Now you can print it using
<img src="<?php print $default_thumbnail; ?>" />

ykyuen’s picture

i am using gravatar integration so i could not get the image path.
is there any other way to get the user picture with style?

tecjam’s picture

So you are using external images?

Maybe try http://drupal.org/project/imagecache_external

ykyuen’s picture

Thanks. it works for gravatar now after using imagecache_external. here is a sample code. i can get the path using the gravatar_get_gravatar()

  $user = user_load($uid);
  print theme('imagecache_external', array('path' => gravatar_get_gravatar($user->mail), 'style_name'=> '<style_name>')); 
mototribe’s picture

is there an option that will also include the correct width and heights from the style?

ykyuen’s picture

the width and height are set in the image style

thimothoeye’s picture

if (list($width, $height, $type, $attributes) = @getimagesize($vars['image_url'])) {
      $vars['width'] = $width;
      $vars['height'] = $height;
    }

As described here

RoastBeats’s picture

I don't know if this will help anyone or if it was just for my use case, but I ended up having to make a slight tweak in the second parameter of the image_style_url, switching:

user->picture[0]['uri']

to this:

user->picture->uri

tecjam’s picture

For finding the correct combination to grab your image(s) the devel module is simply invaluable! =)

kari.nies’s picture

Thanks for your reply, this was very helpful. However I found that it did not cause the images to get regenerated into the cache. Using theme fixed my problem:

echo theme('image_style', array('style_name' => 'Large', 'path' => $user->picture->uri));

youan’s picture

This works great.

However I am having an issue getting it to display the default profile image if one has not been added for the user.

as
$user->picture
is null.

It throws a horrible error
Notice: Trying to get property of non-object in include()

Any ideas how to combat this so that it displays the default image.

Many Thanks

tecjam’s picture

You could add a simple check, eg:

if(isset($user->picture) && $user->picture != '') {
 ... output user image
} else {
 ... output placeholder image
}
youan’s picture

Thanks tecjam, that's the method I ended up using.
I had wondered whether there was a way of having the theme function fall back to the default profile picture, if you told it you were theming a profile picture.

paultrotter50’s picture

After a lot of struggling with this I place this code in my node--article.tpl.php template and it worked fine.

	$user = user_load($uid);
	echo theme('image_style', array('style_name' => 'staff_blog-post', 'path' => $user->picture->uri));
rishi.kulshreshtha’s picture

Hi Kit,

You can surely achieve this in a Drupal way using the following code, I've tried to mention as much as reference I can provide in the code with the help of comments.

// Defining $user in global.
global $user;

// Condition to check whether user is logged in.
if ($user->uid) {
  $user = user_load($user->uid);
  print theme(
    'image_style', // The magic function image_style() for more detail you can check this link https://api.drupal.org/theme_image_style
    array(
      'style_name' => 'thumbnail', // You can choose your own style here from admin/config/media/image-styles
      'path' => !empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default'),
      'attributes' => array(
        'class' => 'avatar' // Your custom class for the <img> tag can be defined here.
      )
    )
  );
}

Cheers!

maen’s picture

Here my solution that works for me:

$output = '<div class="last_active_users_gen">';

I took the user object with db_query ...
and then:

foreach ($results as $record) {// loop for each user I took
            //load each picture and name
            $user_name = $account->name;
            $user_picture = $account->picture;
            if(isset($user_picture)){
            $file = $user_picture->uri;
                $variables= array(
                  'style_name' => 'crop_100',// my image cache
                  'path' =>"$file",
                  'alt' => $user_name,
                  'title' => $user_name,
                  'width' => '50px',
                  'height' => '50px',
                  'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
             );
                $img = theme('image_style', $variables); // here the first part of the if condition Rishi wrote in the array
            }
            else{
                $variables= array(
                 // 'style_name' => 'crop_100', -> no stylename so default works
                  'path' => "sites/default/files/user.png" ,
                  'alt' => $user_name,
                  'title' => $user_name,
                  'width' => '50px',
                  'height' => '50px',
                  'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
             );
                $img = theme('image', $variables); // here the default without image_cache
            }
            
            $output .= '<div class="last_active_users"><div class="last_active_users_img">';        
            $output .= $img.'</div><p class="last_active_users_title">';
            $output .= $user_name.'</p></div>';
            }
            $output .= '</div>';
Bensbury’s picture

Another way to get the default picture with less conditionals is to make the background of the div that contains the user picture the default image in CSS.
Then if the image is absent, it does not render but the background image is shown.
If it renders the background image is covered.

rishi.kulshreshtha’s picture

In your template.php

function YOURTHEMENAME_preprocess_page(&$variables) {
  global $user;
  $user = user_load($user->uid);
  $variables['user_image'] = theme(
    'image_style', array(
      'style_name' => 'thumbnail',
      'path' => !empty($user->picture->uri) ? $user->picture->uri : variable_get('user_picture_default'),
      'attributes' => array(
        'class' => 'your-desired-class-name',
      ),
    )
  );
}

This should go in your .tpl.php file
global $user; if ($user->uid):
echo $user_image;
endif;

chi’s picture

$user_picture_style = $GLOBALS['conf']['user_picture_style'];
$GLOBALS['conf']['user_picture_style'] = 'mini';
theme('user_picture', array('account' => $account,'style_name' => 'mini')),
// Return original style in case there are some other user pictures on the page.
$GLOBALS['conf']['user_picture_style'] = $user_picture_style;

________________________
Override, don't change!