You are here

function simple_fb_connect_get_fb_profile_pic in Simple FB Connect 7.2

Makes an API call to get user's Facebook profile picture.

Parameters

Facebook\FacebookSession $fb_session: FacebookSession object.

Return value

int|false File id (fid) of the Drupal file object if download was successful. False on errors.

1 call to simple_fb_connect_get_fb_profile_pic()
simple_fb_connect_create_user in ./simple_fb_connect.module
Creates a new user account for a Facebook user.

File

./simple_fb_connect.module, line 537
Simple Facebook Login Module for Drupal Sites.

Code

function simple_fb_connect_get_fb_profile_pic(FacebookSession $fb_session) {

  // Get desired dimensions from module settings.
  $dimensions_in_text = variable_get('user_picture_dimensions', SIMPLE_FB_CONNECT_DEFAULT_DIMENSIONS_STRING);
  $dimensions = explode('x', $dimensions_in_text);
  if (count($dimensions) == 2) {
    $width = $dimensions[0];
    $height = $dimensions[1];
  }
  else {
    $width = SIMPLE_FB_CONNECT_DEFAULT_WIDTH;
    $height = SIMPLE_FB_CONNECT_DEFAULT_HEIGHT;
  }

  // Check that target directory is writeable.
  $picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures/');
  if (!file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY)) {
    watchdog('simple_fb_connect', 'Could not save FB profile picture. Directory is not writeable: @picture_directory', array(
      '@picture_directory' => $picture_directory,
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Call Graph API to request profile picture.
  $api_version = simple_fb_connect_get_api_version();
  try {
    $request = new FacebookRequest($fb_session, 'GET', '/me/picture', array(
      'width' => $width,
      'height' => $height,
      'redirect' => 'false',
    ), $api_version);
    $graph_object = $request
      ->execute()
      ->getGraphObject();

    // We don't download the FB default silhouettes, only real pictures.
    $is_default_picture = (bool) $graph_object
      ->getProperty('is_silhouette');
    if ($is_default_picture) {
      return FALSE;
    }

    // Save the picture locally. Use FB user_id as file name.
    $picture_url = $graph_object
      ->getProperty('url');
    $fb_user_id = $fb_session
      ->getSessionInfo()
      ->getProperty('user_id');
    $destination = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures') . '/' . check_plain($fb_user_id) . '.jpg';
    if ($file = system_retrieve_file($picture_url, $destination, TRUE, FILE_EXISTS_REPLACE)) {
      return $file->fid;
    }
    else {
      watchdog('simple_fb_connect', 'Could not save FB profile picture. Check that directory is writeable: @destination', array(
        '@destination' => $destination,
      ), WATCHDOG_ERROR);
    }
  } catch (FacebookRequestException $ex) {
    watchdog('simple_fb_connect', 'Could not load FB profile picture: FacebookRequestException. Error details: @message', array(
      '@message' => json_encode($ex
        ->getResponse()),
    ), WATCHDOG_ERROR);
  } catch (\Exception $ex) {
    watchdog('simple_fb_connect', 'Could not load FB profile picture: Unhandled exception. Error details: @message', array(
      '@message' => $ex
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }

  // Something went wrong and the picture could not be loaded / saved.
  return FALSE;
}