Hi,

How can I load user picture programmatically ı searched at google ı find this code but ı can t load user picture in Drupal 7 help please

$newUser = array(
  'name' => 'username',
  'pass' => 'password', // note: do not md5 the password
  'mail' => 'email address',
  'status' => 1,
  'init' => 'email address'
);           
user_save(null, $newUser);
// load user object
$existingUser = user_load('USERID');

// update some user property
$existingUser->some_property = 'blah';

// save existing user
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);
// load user object
$existingUser = user_load('USERID');

// create an array of properties to update
$edit = array(
  'profile_first_name' => 'Eric'
);

// save existing user
user_save(
  (object) array('uid' => $existingUser->uid),
  $edit,
  'Personal Information' // category
);

Comments

justsomeguy.com’s picture

I would suggest that you install the Devel module (if you haven't already) and look at the data that Drupal is expecting under the user's "Devel -> Dev load" tab.

Hope that helps!

Steve

cumhur’s picture

Thank you Steve I will try

cumhur’s picture

I try but I couldn t find any solution
Where can find a sample code for programaticly upload user picture?

thanks

areynolds’s picture

One way to programmatically save user pictures (if that's what you're trying to do) is after user_save(), manually insert the file into the {users} table.

  if ($account->uid) {
    $picture_directory =  file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
    if(file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY)){
      $picture_result = $your_picture_file_here;
      $picture_path = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '-' . REQUEST_TIME . '.jpg');
      $picture_file = file_save_data($picture_result->data, $picture_path, FILE_EXISTS_REPLACE);
 
      // Check to make sure the picture isn't too large for the site settings.
      $max_dimensions = variable_get('user_picture_dimensions', '85x85');
      file_validate_image_resolution($picture_file, $max_dimensions);
      
      // Update the user record.
      $picture_file->uid = $account->uid;
      $picture_file = file_save($picture_file);
      file_usage_add($picture_file, 'user', 'user', $account->uid);
      db_update('users')
        ->fields(array(
        'picture' => $picture_file->fid,
        ))
        ->condition('uid', $account->uid)
        ->execute();
      $account->picture = $picture_file->fid;
    }
  }

I've had no end of trouble with $account->picture, probably stemming from my ignorance of the File API. It seems that $account->picture is supposed to be a file id (fid), but perhaps you're supposed to pass a complete file object? I've seen people do things like $account->picture->uri, which makes me believe that perhaps it's a complete file object that you're supposed to pass in to programmatically create it. If anyone can give a definitive answer on this, I'd be much obliged.

Note: Code from facebook_oauth module, thanks to quicksketch and others.

--Alec

Tandem

danylevskyi’s picture

Hi! Recently I was working on this problem. I have a solution.
Here you can find an example how to add a picture to the user account programmatically:
http://d.danylevskyi.com/node/7

quinns’s picture

Thank you, your code snippet was just what I needed. Terrific!

james marks’s picture

Here's danylevskyi's solution from the (now expired) link he provided:

$uid = 3;
$account = user_load($uid);

// suppose that a source user picture is located in the 'sites/default/files/upics' directory.
// get image information
$image_path = 'public://upics/userpic.jpg';
$image_info = image_get_info($image_path);
  
// create file object
$file = new StdClass();
$file->uid = $uid;
$file->uri = $image_path;
$file->filemime = $image_info['mime_type'];
$file->status = 0; // Yes! Set status to 0 in order to save temporary file.
$file->filesize = $image_info['file_size'];

// standard Drupal validators for user pictures
$validators = array(
  'file_validate_is_image' => array(),
  'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
  'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);

// here all the magic :)  
$errors = file_validate($file, $validators);
if (empty($errors)) {
  file_save($file);
  $edit['picture'] = $file;
  user_save($account, $edit);
}
ltiong’s picture

// Attaching User Picture upload to form
function somemodule_form($form, &$form_state) {
  ...
  // Attach User Picture upload
  $form['picture'] = array('#weight' => 130);
  $form['picture']['picture_upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload picture'),
    '#size' => 48,
    '#description' => 'Some description',
  );
  $form['#validate'][] = 'user_validate_picture';
  ...
}
// Handling Submission/Save
function somemodule_form_submit($form, &$form_state) {
  global $user;
  $account = user_load($user->uid);
   ...
  // Save picture
  if(!empty($form_state['values']['picture_upload'])){
    $edit = array(
      'picture' => $form_state['values']['picture_upload'],
    );
    user_save($account, $edit);    
  }
  ...
}