如何使用PHP更改Facebook个人资料图片

时间:2016-05-07 06:43:08

标签: php facebook facebook-canvas

我想为一个原因制作一个Facebook画布应用程序,我需要在他/她的许可下更改用户的个人资料照片,我已经用选定的视口获取了用户的个人资料照片并放置了一个覆盖图因为&makeprofile=1无效,它会将我重定向到影院模式的照片。我想要这样的东西app

<?php
session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new Facebook\Facebook([
  'app_id' => 'app',
  'app_secret' => 'secret',
  'default_graph_version' => 'v2.6',
]);
$helper = $fb->getCanvasHelper();
$permissions = ['email','publish_actions']; // optionnal
try {
    if (isset($_SESSION['facebook_access_token'])) {
    $accessToken = $_SESSION['facebook_access_token'];
    } else {
        $accessToken = $helper->getAccessToken();
    }
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
 }
if (isset($accessToken)) {
    if (isset($_SESSION['facebook_access_token'])) {
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    } else {
        $_SESSION['facebook_access_token'] = (string) $accessToken;
        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }
    // validating the access token
    try {
        $request = $fb->get('/me');
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        if ($e->getCode() == 190) {
            unset($_SESSION['facebook_access_token']);
            $helper = $fb->getRedirectLoginHelper();
            $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/my-first-qode/', $permissions);
            echo "<script>window.top.location.href='".$loginUrl."'</script>";
            exit;
        }
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // getting basic info about user
    try {
                $requestPicture = $fb->get('/me/picture?redirect=false&width=400&height=400');
        $profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
                $picture = $requestPicture->getGraphUser();
        $profile = $profile_request->getGraphNode()->asArray();
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        unset($_SESSION['facebook_access_token']);
        echo "<script>window.top.location.href='https://apps.facebook.com/my-first-qode/'</script>";
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // priting basic info about user on the screen
    print_r($profile);


echo $profile['name'];
echo "<img src='".$picture['url']."'/>";


//image create
$base_image = imagecreatefrompng("mark.png");
        // Get the facebook profile image in 200x200 pixels
        $photo = imagecreatefromjpeg($picture['url']);
        //$photo = imagecreatefromjpeg("http://graph.facebook.com/".$id."/picture?width=200&height=200");

        //resizeImage($photo,920,920);
        // read overlay  
        $overlay = imagecreatefrompng("mark.png");
        // keep transparency of base image
        imagesavealpha($base_image, true);
        imagealphablending($base_image, true);
        $lwidth  = imagesx($photo);
        $lheight = imagesy($photo);

        // place photo onto base (reading all of the photo and pasting unto all of the base)
        imagecopyresampled($base_image, $photo, 0, 0, 0,0, 200, 200, $lwidth, $lheight);

        // place overlay on top of base and photo
        imagecopy($base_image, $overlay, 0, 0, 0, 0, 200, 200);
        // Save as jpeg
        imagejpeg($base_image,'sample3.jpg');

//image create
echo '<img src="sample3.jpg" alt="photo_stamp1" />';

/*$data = [
  'message' => 'My awesome photo upload example.',
  'source' => $fb->fileToUpload('252676_368007166708864_8124350312635670437_n.jpg'),
  // Or you can provide a remote file location
  //'source' => $fb->fileToUpload('https://example.com/photo.jpg'),
];

try {
  $response = $fb->post('/me/photos', $data);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Error: ' . $e->getMessage();
  exit;
}

$graphNode = $response->getGraphNode();

echo 'Photo ID: ' . $graphNode['id'];

*/   

    // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
    $helper = $fb->getRedirectLoginHelper();
    $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/my-first-qode/', $permissions);
    echo "<script>window.top.location.href='".$loginUrl."'</script>";
}

1 个答案:

答案 0 :(得分:0)

您无法直接更改个人资料照片,就像在其他应用中完成的那样。您必须将用户重定向到上传的照片,然后他可以选择将其作为自己的个人资料图片。庆祝骄傲App来自Facebook。