Developing Facebook Applications to Upload Images (Using Graph API & php SDK)

Check this first

It is some time ago i wrote my last post facebook development and by that time many new articles have pop up on the uses of graph API.

In this post i will go through how to upload a photo thruogh your facebook app and later on how to tag it.
I think everyone has experienced some where an app which selects “top friends”, “best friends” etc etc and pops up an image with ourselves tagged. Honestly it can be sucha nuisance that you hate to see. If you log into facebook after some time,you might be wondering how you got tagged to soo many pics.

In this code i assume that you have prepared th image to be uploaded by doing all edits. If you have no clue on have to get images from facebook, you should look into CURL. And for editing GD.

First of all to publish an image the user need to provide “Publish Stream” permission during authentication stage. See my previous post on facebook application Authentication.

You can publish a photo to app users profile with a POST to graph API photos as below:

$facebook->api('/me/photos', 'POST', $image);


Before doing this in php SDK set file upload permissions as follows:
$facebook->setFileUploadSupport(true);

The $image is an array with photo details. ‘access_token’ should be provided to see whether a valid session is present. ‘message’ is the message or the desription that should be added to the image. I am using a variable $mess. ‘image’ should carry the path of the image to be uploaded.’@’.realpath($path); creates a curl-style upload path.

$image = array(
				'access_token' => $session['access_token'],
				'message' => $mess,
			);

			$image['image'] = '@'.realpath($path);

Well thats it for image upload. Facebook will create an album with your app name if you dont provide with a album ID. Else you can upload the image to an already existing album using the album ID. Try is your self :)

This entry was posted in Facebook, php. Bookmark the permalink.

3 Responses to Developing Facebook Applications to Upload Images (Using Graph API & php SDK)

  1. Paul says:

    I have this working locally, but now I’ve gotta make it work for images that are generated dynamically by another script… so realpath() won’t help. I tried just referring to the public url of the resulting image but no luck (it doesn’t physically exist on the hdd). Any ideas?

  2. admin says:

    Try this:
    http://php.net/manual/en/function.imagejpeg.php

    use imagejpeg($image, $path) function to create the image. Pass the Image resource file you created from the other script as $image while $path for where you want to save it. then use above to upload :)

    GD library will be highly helpful.

  3. sakthi says:

    I’m still confused about the path ! Can you please post a sample code which you’ve used to post photos on profile.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>