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.