Naga Raju
Naga Raju

Reputation: 1

How to display the Facebook profile photo in my php project

In my project I am signing in with a Facebook account, but the profile page displays the image from gravatar.com. I want to display the profile image from www.facebook.com of registered users. In my local system I have the following code to display profile photo from www.gravatar.com

public function gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = FALSE, $atts = array())
{
    $url = 'https://secure.gravatar.com/avatar/'
        . md5(strtolower(trim( $email)))
        . "?s=$s&d=$d&r=$r";

    if ($img)
    {
        $url = '<img src="' . $url . '"';
        foreach ($atts as $key => $val)
        {
            $url .= ' ' . $key . '="' . $val . '"';
        }
        $url .= ' />';
    }
    return $url;
}

How can I display the Facebook profile photo instead of gravatar.com profile photo?

Upvotes: 0

Views: 1470

Answers (3)

Sabari
Sabari

Reputation: 6335

If you are using PHP SDK you can get the userId of the current logged in USer and display the user Image.

You can do like this:

require_once 'path/to/facebookSDK.php';

$facebook = new Facebook(array(
       'appId'  => 'YOUR_APP_ID',
       'secret' => 'YOUR_APP_SECRET',
));

// Get User ID
$user = $facebook->getUser();

//this will ouptput your current logged in user facebook image
echo getFacebookPhotoAvatar ($user);

function getFacebookPhotoAvatar ($user) {
      if($user) {
            $photoUrl = "https://graph.facebook.com/".$user."/picture";
            $facebookPhoto = '<img src="'.$photoUrl.'" alt="FacebookUserImage"/>';
      } else {
            $facebookPhoto = '';
      }

      return $facebookPhoto ;
}

Hope this helps :)

Upvotes: 0

ahmet2106
ahmet2106

Reputation: 5007

Something like this:

function facebookAvatar($uid, $img = FALSE, $atts = array())
{
    $url = 'https://graph.facebook.com/'.trim($uid).'/picture';

    if ($img)
    {   
        $url = '<img src="' . $url . '"';
        foreach ($atts as $key => $val)
        {
            $url .= ' ' . $key . '="' . $val . '"';
        }
        $url .= ' />';
    }
    return $url;
}

and to get the Image use:

<?php echo facebookAvatar('UsernameOrUserID', TRUE, array('width'=>'80px', 'height'=>'80px')); ?>

UsernameOrUserID you've to change to the UserID:

1234321

if the url is:

http://facebook.com/profile.php?id=1234321

or to the username:

my.name

if the facebook url is http://facebook.com/my.name

Upvotes: 0

AppleGrew
AppleGrew

Reputation: 9570

You can link to https://graph.facebook.com/usernameOrId/picture.

Upvotes: 1

Related Questions