4

i am trying to fetch loged in users photo to make a welcome block. i have applied a code which is not working

$userphoto = $user->picture;

so what to do to fetching user photo or image URL, any idea?

5

The picture property of the user entity is the file ID (a reference to the fid column in the file_managed table).

You can load the file explicitly with file_load() like so:

$file = file_load($user->picture);

And render it to an image using theme_image():

$image = theme('image', array(
  'path' => $file->uri,
  'alt' => 'Some alt',
  // etc...
));

Alternatively you could dispense with the above and just use the handy theme_user_picture() function:

$image = theme('user_picture', array('account' => $account));
| improve this answer | |
  • thanks for that, but is it possible to get the image path(URL)? – rakibtg Jul 16 '12 at 15:02
  • 1
    Sure, just run $url = file_create_url($file->uri);, that'll give you a web-accessible URL as opposed to a stream URI – Clive Jul 16 '12 at 15:03
  • oh is it my mistakes, it shows only home page address with this error message: ` Notice: Undefined variable: file in eval() (line 4 of /home/busines/public_html/modules/php/php.module(80) : eval()'d code). Notice: Trying to get property of non-object in eval() (line 4 of /home/busines/public_html/modules/php/php.module(80) : eval()'d code). ` – rakibtg Jul 16 '12 at 15:15
  • Have you put $file = file_load($user->picture); before you run that line? – Clive Jul 16 '12 at 15:15
  • Thank you so much Clive .. works great now, i will write again if any problem happens :) – rakibtg Jul 16 '12 at 15:18
1

Maybe it is helpful to render with a image styles

image_style_url('my_image_style',$user->picture['uri']);

second param is a drupal's internal file path like 'public://images/myimage.png'

| improve this answer | |
0

You can try the following:

$user = user_load($view->result[$key]->uid);
$filepath = $user->picture->uri;
$picture_url = file_create_url($filepath);

This will give you the user's picture url.

| improve this answer | |

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.