Upload base64 encoded images using Facebook sdk?
-
26-10-2019 - |
Pergunta
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",
));
Solução
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.
Outras dicas
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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow