PHP Change Facebook Profile Picture With Graph API
Posted by in Facebook Graph API November 15, 2011 4 Comments

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>";
?>

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.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.2jdesign.co.uk/ Website Design Sheffield

    A piece of erduiiotn unlike any other!

  • Adi

    Hi,
    Thank you for this article, really helpful. I have a question though, is there any possibility to upload an image though FB.api by URL? For example using a cdn for photos.

    Thanks,
    Adi

  • Kei

     This is very useful.
    I tested this by
    PC browser : work fine
    iPhone Safari : not work. I think because useragent.
    iPhone webview : not work. already changed useragent but still not work.
    iPad Safari : work fine
    iPad webview : not work. I think it’s the same issue as iPhone webview.

    anyone know what’s the problem? and how to run this php on webview?

  • Stew

    Hi
    How i can edit your script for upload selected images from mine web site?