I am using the following code.

if ($logged_in) {
  $user = user_load($user->uid);
  print theme('image_style', array('style_name' => 'thumbnail_scale_n_crop_120_x_120', 'path' => $user->picture->uri));

It displays the user's picture.

How should I modify it so that so that when the user has NOT uploaded or has deleted his picture a standard "no photo" picture is displayed instead as in:

$no_avi = "http://example.com/sites/default/files/nophoto.jpg";
echo("<img src=\"$no_avi\" />");

The User Picture Field module does not provide this capability.


Basing on the code used by template_preprocess_user_picture(), I would suggest the following code.

if (!empty($user->picture)) {
  if (is_numeric($user->picture)) {
    $account->picture = file_load($user->picture);
  if (!empty($user->picture->uri)) {
    $filepath = $user->picture->uri;
elseif (variable_get('user_picture_default', '')) {
  $filepath = variable_get('user_picture_default', '');
if (isset($filepath)) {
  $alt = t("@user's picture", array('@user' => format_username($account)));
  if (module_exists('image') && file_valid_uri($filepath)) {
    $user_picture = theme('image_style', array('style_name' => 'thumbnail_scale_n_crop_120_x_120', 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
  else {
    $user_picture = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));

If you didn't need to use a specific style, but you could use the one set in Drupal settings, the code would simply be the following one.

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

Keep in mind that there are some places where Drupal already shows the user's picture. If you just need to change the image style used for the user's picture, that is possible from Drupal settings (see this other answer). If you need to show the user's picture in a page where Drupal doesn't show them, I would set the default image style for the user's picture in Drupal's admin settings, and use the following code.

$user_picture = theme('user_picture', array('account' => $user));
| improve this answer | |
  • Great! I didn't try your first solution as the second solution worked. All I had to do is to set the newly created image field (for User Picture Field) in the account settings to "no preview" and to use some css to reposition the image. Thank you so much! :) – user14666 Dec 10 '14 at 17:22

Why would you want to upload programmatically, Drupal provide this functionality for default picture.

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

| improve this answer | |
  • The user is using code to visualize a user's picture, and wants to show the default picture when the users don't have a picture. The OP is not talking about programmatically uploading a picture. – kiamlaluno Dec 8 '14 at 5:41
  • ok. That is my suggestion, to use this. – Nishant Dec 8 '14 at 5:51
  • The default Drupal "nophoto" picture is great but I need to use the User Picture Field module. It gives a lot more flexibility in handling users' pictures throughout the site but it strips the "nophoto.jpg" picture from the user's account edit page. The displayed "nophoto" is important there to remind the user to upload a picture. Also, my views are set to display only users that have an avatar picture. Doing this any other way reintroduces the "nophoto" in views as the filter no longer works. The "nophoto" pic is treated like a normal picture and that's a no-no on my site. :). – user14666 Dec 8 '14 at 17:47
  • So, the only way to do this is to programmatically display the "nophoto" default picture on the account edit page, when the user doesn't have a pic, using the template for that page. But thank you for your interest and your suggestion. – user14666 Dec 8 '14 at 17:55
  • you welcome @user14666 – Nishant Dec 9 '14 at 4:43

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.