Community

programmatically assign user pictures to a user field

i updated a website to d7, and user pictures were deleted (changed the field type etc etc, an accident).
but i wish to update the existing users with their photos again. i have the old directory of images which are fortunately named using the uid, so i can match them to existing users.
i have user images now as a field on the user entity (field_profile_photo).
how can i programmatically assign an image to a user entity field? i tried this from googling:

    if($uid) {
      $account=user_load($uid);
      // get image information
      $image_path = $file->uri;
      $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 = array();
        $edit['field_profile_photo'][LANGUAGE_NONE][0] = $file;
        user_save($account, $edit);
      }
      dpm($account);
    }

but no joy. website crashes when i run this. note if i use $edit['picture'] it works and assigns the image to the picture field, but it renames the image.
any help would be appreciated
nobody click here