2

I want to display programmatically default user picture if the user is not uploaded any. For that I have written code php code but its displaying user uploaded user picture correctly but not the one default when user doesn't upload his/her own. How to do that? help me.

$user = user_load($user->uid);
$account = user_load($user->uid);
print theme(
    'image_style',
    array( 'account' =>$user,
      'style_name' => 'x-small', 
     // 'path' =>!empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default'),
      'path' =>!empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default',''),
      'attributes' => array(
        'class' => 'avatar' 
      )
    )
  );
1

Why programmatically drupal provide this functionality out of the box. Go to Configuration >> People >> Account Settings. Under personalisation section you can provide the url of the default picture and can select the image style to use for the picture.

enter image description here

2

Its very straight forward. If you want to add a default picture then go ahead and add the picture from the admin page as the other answer suggested. Programmatically, The default picture is loaded via the variable user_picture_default. So you can do,

$user = user_load($uid);
print theme('image_style', array('path' => $user->picture->uri, 'style_name' => '<image-style>'));

if (!empty($user->picture)) {
  print theme('image_style', array('path' => $user->picture->uri, 'style_name' => 'CUSTOM_STYLE'));
}
else {
  $custom_default_image_path = 'public://default_image.png';
  print theme('image_style', array('path' => variable_get('user_picture_default', $custom_default_image_path), 'style_name' => 'CUSTOM_STYLE'));
}

However, you can also make the default image theme dependent/independent via the hook

0

First you need to assign a default picture path in Account settings in the drupal admin as described by Mutant Mahesh .

Then you can use this code to print picture anywhere in your code

<?php
global $user;
  $user = user_load($user->uid);
  print theme('user_picture', array('account' =>$user));
?>

Hope it will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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