You are here

function openid_connect_save_user_picture in OpenID Connect / OAuth client 7

Save an image as the user picture.

Parameters

object $account: The user account.

string $picture_url: The URL to a user picture.

1 call to openid_connect_save_user_picture()
openid_connect_save_userinfo in ./openid_connect.module
Saves user profile information into a user account.

File

./openid_connect.module, line 267
A pluggable client implementation for the OpenID Connect protocol.

Code

function openid_connect_save_user_picture($account, $picture_url) {
  $picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
  if (!file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY)) {
    return;
  }
  $response = drupal_http_request($picture_url);
  if ($response->code != 200) {
    watchdog('openid_connect', 'The user picture could not be fetched from URL: @url', array(
      '@url' => $picture_url,
    ));
    return;
  }

  // Skip saving if the remote picture has not changed.
  $hash = md5($response->data);
  if (!empty($account->picture) && isset($account->data['oidc_picture_hash']) && $account->data['oidc_picture_hash'] === $hash) {
    return;
  }
  $picture_path = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '-' . REQUEST_TIME . '.jpg');
  $picture_file = file_save_data($response->data, $picture_path, FILE_EXISTS_REPLACE);

  // Check to make sure the picture isn't too large for the site settings.
  // Suppress the status message that Drupal sets after a successful resizing.
  $status_messages = isset($_SESSION['messages']['status']) ? $_SESSION['messages']['status'] : NULL;
  file_validate_image_resolution($picture_file, variable_get('user_picture_dimensions', '1024x1024'));
  if (isset($status_messages)) {
    $_SESSION['messages']['status'] = $status_messages;
  }
  else {
    unset($_SESSION['messages']['status']);
  }

  // Update the user account object.
  $account->picture = $picture_file;
  $account->data['oidc_picture_hash'] = $hash;
  user_save($account);
}