如何使用 php 更改 Facebook 个人资料图片
How to change facebook profile pic using php
我想制作一个 Facebook canvas 应用程序,我需要在 his/her 许可下更改用户的个人资料图片,我已使用选定的视口获取用户的个人资料图片并放置该原因的叠加但 &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>";
}
您不能像在其他应用程序中那样直接更改个人资料图片。您必须将用户重定向到上传的照片,然后他可以选择将其设为自己的头像。 celebrate pride App 来自Facebook.
我想制作一个 Facebook canvas 应用程序,我需要在 his/her 许可下更改用户的个人资料图片,我已使用选定的视口获取用户的个人资料图片并放置该原因的叠加但 &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>";
}
您不能像在其他应用程序中那样直接更改个人资料图片。您必须将用户重定向到上传的照片,然后他可以选择将其设为自己的头像。 celebrate pride App 来自Facebook.