You are here

function commons_set_default_avatar in Drupal Commons 7.3

Set a default user avatar as a managed file object.

2 calls to commons_set_default_avatar()
commons_install in ./commons.install
Implements hook_install().
commons_update_3102 in ./commons.install
Reset the default avatar if the associated file doesn't exist in the file_managed table.

File

./commons.install, line 563
Install, update and uninstall functions for the Commons install profile.

Code

function commons_set_default_avatar() {
  global $base_url;
  $picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
  if (file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY)) {
    $picture_result = drupal_http_request($base_url . '/profiles/commons/images/avatars/user-avatar.png');
    $picture_path = file_stream_wrapper_uri_normalize($picture_directory . '/picture-default.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.
    $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,
      ),
    );

    // attach photo to user's account.
    $errors = file_validate($picture_file, $validators);
    if (empty($errors)) {

      // Update the user record.
      $picture_file = file_save($picture_file);
      variable_set('user_picture_default', $picture_path);
    }
  }
}