Skip to content

Commit

Permalink
Added method getPhoto for users.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffhodsdon committed Aug 29, 2008
1 parent 60b63e9 commit 1c79d9d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Services/Facebook/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class Services_Facebook_Users extends Services_Facebook_Common
'wall_count', 'work_history'
);

/**
* photoSizes
*
* @var array $photoSizes Supported photo sizes
* @see self::getPhoto
*/
protected $photoSizes = array('big', 'small', 'square');

/**
* Has the current user added this application?
*
Expand Down Expand Up @@ -160,6 +168,49 @@ public function hasAppPermission($perm)

return (intval((string)$result) == 1);
}

/**
* Get photo
*
* Get a photo given an user id. Allow different sizes.
*
* @param int $uid Id of the user you want to get a photo of
* @param string $size Size of the photo {@link self::photoSizes}
*
* @return mixed Photo data
*/
public function getPhoto($uid, $size = '')
{
$field = 'pic';
if ($size !== '') {
if (!in_array($size, $this->photoSizes)) {
throw new Services_Facebook_Exception('Photo size "' .
$size . '" is not supported.');
}

$field .= '_' . $size;
}

$url = (string) $this->getInfo($uid, array($field))->user->$field;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Services_Facebook::$timeout);
$photo = curl_exec($ch);

if (curl_errno($ch)) {
throw new Services_Facebook_Exception(
curl_error($ch),
curl_errno($ch)
);
}
curl_close($ch);

return $photo;
}
}

?>

0 comments on commit 1c79d9d

Please sign in to comment.