4

When i visualize user avatar in Drupal 7 using this code:

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

The following image is generated:

<a href="/drupal/en/user/2" title="View user profile."><img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/styles/thumbnail/public/pictures/picture-2-1303917742.jpg" alt="ksu's picture" title="ksu's picture"></a>

How can i control such parameters as size of the image, title, alt? And how can i get rid of the link (or change the path) which is generated automatically with the image?

<a href="/drupal/en/user/1" title="View user profile.">...<a/>

3 Answers 3

5

You should use theme_image_style instead.

**Update:

Example setting width/height attributes by hand (until it's resolved in Drupal core):

global $user;
$user = user_load($user->uid);
print theme('image_style', array( 'path' =>  $user->picture->uri, 'style_name' => 'thumbnail', 'width' => '150', 'height' => '162'));

Remember to change your image style 'thumbnail' in this case to your desired width/height. Now that I think about it you don't necessarily need to use theme_image_style, you could just edit the image style thumbnail presets and get the desired results. If you want width/height HTML attributes, use the example above.

3
  • Thank you, but could you explain how to use it? In the link you provided it's not clear explained.
    – Alexey
    Aug 2, 2011 at 9:30
  • Right now the width/height attributes has still not been properly solved in Drupal 7 core: drupal.org/node/1129642 - For now you need to can set those attributes manually though.
    – ipwa
    Aug 7, 2011 at 9:35
  • I updated my answer with an example.
    – ipwa
    Aug 7, 2011 at 9:44
3

Here is my workaround:

$GLOBALS['conf']['user_picture_style'] = 'PRESET NAME';
$user_picture = theme('user_picture', array('account' => $account));

To change user picture style in node template

/**
 * Override or insert vars into the node template.
 */
function HOOK_preprocess_node(&$vars) {
  if ($vars['user_picture'] && $vars['display_submitted']) {
    $GLOBALS['conf']['user_picture_style'] = 'PRESET NAME';
    $vars['user_picture'] = theme('user_picture', array('account' => $vars['node']));
  }
}

To change user picture style in comment template

/**
 * Override or insert vars into the comment template.
 */
function HOOK_preprocess_comment(&$vars) {
  if ($vars['picture']) {
    $GLOBALS['conf']['user_picture_style'] = 'PRESET NAME';
    $vars['picture'] = theme('user_picture', array('account' => $vars['comment']));
  }
}

Note: To display original image use empty string as preset name.

1

You can try this!!

global $user;
$user_item = user_load( $user->uid );

 print theme('user_picture', array('account' =>$user_item ,'style_name' => 'thumbnail', 'width' => '150', 'height' => '162')); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.