Drupal: checking if a user has an image

I am showing userpicture on node with this code here:

<?php $user_load = user_load($node->uid); $imgtag = theme('imagecache', 'avatar_node', $user_load->picture, $user_load->name, $user_load->name); $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); print l($imgtag, 'u/'.$user_load->name, $attributes); ?> 

This works great if the user has no image, in which case it looks weird.

How to load a default image if the user does not have an image. I do not believe that I have access to $ picture in this block.

Thanks in advance.

+4
source share
3 answers
 $user_load = user_load($node->uid); $picture = $user_load->picture ? $user_load->picture : variable_get('user_picture_default', ''); // $imgtag = theme('imagecache', 'avatar_node', $picture, $user_load->name, $user_load->name); $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); print l($imgtag, 'u/'.$user_load->name, $attributes); 

if you copy the default directory to the file directory, you can define it through http://api.drupal.org/api/function/file_directory_path/6

+6
source

This is what I use for a similar situation:

  if (!empty($user->picture) && file_exists($user->picture)) { $picture = file_create_url($user->picture); } else if (variable_get('user_picture_default', '')) { $picture = variable_get('user_picture_default', ''); } if (isset($picture)) { $alt = t("@user picture", array('@user' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous')))); $picture = theme('image', $picture, $alt, $alt, '', FALSE); if (!empty($user->uid) && user_access('access user profiles')) { $attributes = array( 'attributes' => array('title' => t('View user profile.')), 'html' => TRUE, ); echo l($picture, "user/$user->uid", $attributes); } } 

It is adapted from http://api.drupal.org/api/drupal/modules%21user%21user.module/function/template_preprocess_user_picture/6

+2
source

In Drupal 7, I use:

 global $user; file_create_url( file_load($user->picture)->uri) 
+1
source

All Articles