phpfacebookfacebook-canvas

How to change facebook profile pic using php


I want to make a Facebook canvas app for a cause, where I need to change the profile pic of user with his/her permission, I have fetched user's profile pic with selected viewport and placed an overlay of that cause but &makeprofile=1 is not working it redirects me to pic in theater mode. I want something like this 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>";
}

Solution

  • You can´t change the profile picture directly, like it is done in that other App. You MUST redirect the user to the uploaded photo and he can then choose to make it his profile picture on his own. The celebrate pride App is from Facebook.