gremo
gremo

Reputation: 48439

Upload base64 encoded images using Facebook sdk?

It's possible to upload base64 encoded images directly without saving it using Facebook PHP SDK 3.1.1?

$facebook->setFileUploadSupport(true);
$facebook->api('/me/photos', 'POST', array(
    'source' => '@/mycoolpic.png', // No need to use FS, base64 encoded image
    'message' => "I'm cool",
));

Upvotes: 1

Views: 1568

Answers (2)

Gueno
Gueno

Reputation: 197

You can do as follow in PHP :

function base64_to_jpeg( $base64_string, $output_file ) {
  $ifp = fopen( $output_file, "wb" ); 
  fwrite( $ifp, base64_decode( $base64_string) ); 
  fclose( $ifp ); 
  return( $output_file ); 
}
$facebook->setFileUploadSupport(true);
$image = base64_to_jpeg( $your_base64_string, 'tmp.jpg' );
$args = array('message' => 'Some message');
$args['image'] = '@' . realpath( $image );
$data = $facebook->api('/your_user_id/photos', 'post', $args);
unlink($image);

Upvotes: 1

user1074324
user1074324

Reputation:

No, not that I have been aware of, or found out searching. Make sure you use realpath() around the image name, to give the absolute path to the image.

Upvotes: 0

Related Questions