Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Facebook Graph API PHP Change Facebook Profile Picture With Graph API

PHP Change Facebook Profile Picture With Graph API

I’m working on a Facebook application that allows people be able to upload their photo through the app and then if they click on ‘Make Facebook Profile Picture’ button, their Facebook profile picture will be changed automatically.

Actually, it is not possible to change the profile picture directly via Facebook Photo Graph API as no section mention about that.

However, we can do a trick by uploading user’s photo to Facebook via the API then redirect the user to uploaded photo URL with 1 added in querystring parameter as below:

http://www.facebook.com/photo.php?pid=xyz&id=abc&makeprofile=1

“&makeprofile=1” is the main thing here and xyz/abc will be returned by Facebook. By adding the parameter, Facebook will auto change the profile picture of the current user with the uploaded picture above.

PHP change Facebook profile picture

I assume that we already have an image on your server. In real case, we can download/save an external image via url or from user uploading. Our job is just upload it to Facebook then redirect to the uploaded photo url with “makeprofile=1” parameter.

<?php
	ini_set('display_errors', 1);
	error_reporting(E_ALL);
 
	require 'src/facebook.php';
 
	$facebook = new Facebook(array(
		'appId'  => "_your_facebook_app_id_",
		'secret' => "_your_facebook_app_secret_",
		"cookie" => true,
		'fileUpload' => true
	));
 
	$user_id = $facebook->getUser();
 
	if($user_id == 0 || $user_id == "")
	{
		$login_url = $facebook->getLoginUrl(array(
		'redirect_uri'         => "http://apps.facebook.com/rapid-apps/",
		'scope'      => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
					user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));
 
		echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
		exit();
	}
 
	//get profile album
	$albums = $facebook->api("/me/albums");
	$album_id = ""; 
	foreach($albums["data"] as $item){
		if($item["type"] == "profile"){
			$album_id = $item["id"];
			break;
		}
	}
 
	//set photo atributes
	$full_image_path = realpath("Koala.jpg");
	$args = array('message' => 'Uploaded by 4rapiddev.com');
	$args['image'] = '@' . $full_image_path;
 
	//upload photo to Facebook
	$data = $facebook->api("/{$album_id}/photos", 'post', $args);
	$pictue = $facebook->api('/'.$data['id']);
 
	$fb_image_link = $pictue['link']."&makeprofile=1";
 
	//redirect to uploaded photo url and change profile picture
	echo "<script type='text/javascript'>top.location.href = '$fb_image_link';</script>";
?>

<?php ini_set('display_errors', 1); error_reporting(E_ALL); require 'src/facebook.php'; $facebook = new Facebook(array( 'appId' => "_your_facebook_app_id_", 'secret' => "_your_facebook_app_secret_", "cookie" => true, 'fileUpload' => true )); $user_id = $facebook->getUser(); if($user_id == 0 || $user_id == "") { $login_url = $facebook->getLoginUrl(array( 'redirect_uri' => "http://apps.facebook.com/rapid-apps/", 'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos, user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos")); echo "<script type='text/javascript'>top.location.href = '$login_url';</script>"; exit(); } //get profile album $albums = $facebook->api("/me/albums"); $album_id = ""; foreach($albums["data"] as $item){ if($item["type"] == "profile"){ $album_id = $item["id"]; break; } } //set photo atributes $full_image_path = realpath("Koala.jpg"); $args = array('message' => 'Uploaded by 4rapiddev.com'); $args['image'] = '@' . $full_image_path; //upload photo to Facebook $data = $facebook->api("/{$album_id}/photos", 'post', $args); $pictue = $facebook->api('/'.$data['id']); $fb_image_link = $pictue['link']."&makeprofile=1"; //redirect to uploaded photo url and change profile picture echo "<script type='text/javascript'>top.location.href = '$fb_image_link';</script>"; ?>

In the example above, we will upload a photo to the “Profile Pictures” album of user and it’s my recommendation as it may doesn’t work if you try to create a new album then upload photo to there.

Note:

  • 1. In order to upload to Facebook, we need to add 2 settings: cookie = true and fileUpload = true (line: 10,11) and don’t forget to replace appId and secret with yours.
  • 2. The whole process may take a moment depend on how big the uploaded photo is.
  • 3. The process can’t be completed if user close the browser at the time we redirect to Facebook (Photo page). It just completes when the user be redirected from Photo page to their Wall page.

+ View demonstration on my Facebook page – Auto Change Facebook Profile Picture link
+ Download the PHP source code above as well as the current Facebook PHP SDK and one photo.

Nov 15, 2011Hoan Huynh
Virtualmin Upgrade PHPPHP Get Likes Number Of Facebook Page
You Might Also Like:
  • Load And Save Facebook Profile Picture Of User
  • Facebook Load User Profile Via Graph API And FQL Query
  • Most Popular Video And Photo File Extension
  • Get or Find Facebook Profile Id Number
  • Facebook Removed View App Profile Page link For New Apps
  • Facebook Publish To Wall With Popup Or Dialog And Call Back
  • Facebook Publish To Wall With External Link And Track Callback
  • PHP Get Likes Number Of Facebook Page
  • C# Check File Extension In Array Of Valid Extension
  • Add More Extra Informations Or Fields To WordPress User Profile
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago Facebook Graph APIfileUpload, makeprofile, realpath6,073
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,554 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,892 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,745 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,069 views
C# Read Json From URL And Parse/Deserialize Json
11,802 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development