I'd like to print a user's image in a View's template file. If the user hasn't set an image yet, the default user picture (set to public://pictures/default.jpg at Configuration > People > Account settings) should be rendered.

I used the following code:

<?php $account = user_load($user->uid); ?>
<?php if (isset($account->picture->uri)) {
          $path = $account->picture->uri; }
      else {$path = "public://pictures/default.jpg"; } ?>
<?php $array = array('style_name' => 'picture',
                     'path' => $path,
                     'alt' => 'Picture'); ?>
<div class="user-image"><?php print theme('image_style', $array); ?></div>

That works perfectly, but I don't really like the isset approach, because it makes me think that it was redundant to specify the default image path in the configurations. Is there another method that uses the default path automatically?


2 Answers 2


It's a bit of a strangled route - Drupal itself uses some conditionals in template_preprocess_user_picture() to make that decision:

if (!empty($account->picture)) {
  // @TODO: Ideally this function would only be passed file objects, but
  // since there's a lot of legacy code that JOINs the {users} table to
  // {node} or {comments} and passes the results into this function if we
  // a numeric value in the picture field we'll assume it's a file id
  // and load it for them. Once we've got user_load_multiple() and
  // comment_load_multiple() functions the user module will be able to load
  // the picture files in mass during the object's load process.
  if (is_numeric($account->picture)) {
    $account->picture = file_load($account->picture);
  if (!empty($account->picture->uri)) {
    $filepath = $account->picture->uri;
elseif (variable_get('user_picture_default', '')) {
  $filepath = variable_get('user_picture_default', '');

If it suits your use case, you can just set the user_picture_style variable to your style name, and use theme_user_picture():

$build = array(
  '#theme' => 'user_picture',
  '#account' => $user,
$picture = drupal_render($build);

The caveat is that user_picture_style is a global var, so if you want to use something different for the style used elsewhere in the site, you're stuck with reproducing the same logic as the preprocess function above.


From Clive's answer, I got the solution to render node author's image using the below codes:

First of all, we need to get the uid

$node = $variables['node'];
$author = user_load($node->uid);

Then using the uid we can render the user's image like this

<img src="<?php print file_create_url($author->picture->uri); ?>" alt="user picture" />

To get other fields of a user account we have to use field_get_items

$field = field_get_items('user', $author, 'field_mac_name');

then render the fields using print render($field[0]['value']);

Your Answer

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

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