Facebook stream.publish with image attachment from Base64

I am trying to get facebook stream.publish to make a wall post that includes some custom text and a dynamically generated image from the site. The image is only available as Base64, as it was drawn by the user before the publication action. Facebook doesn't seem to like it when src is passed as a Base64 string. Does anyone know a workaround, or I will be forced to first save the image on the server and then provide a link (I really would not have done that).

+3
source share
2 answers

You cannot transfer a base64 image to Facebook in JavaScript. You will need to send it to your server and convert it to png / jpeg or something else and upload it to Facebook ( user rights are required here ). Either this, or save it to the server, and then use the URL that will serve png / jpg in JavaScript.

+3
source

In PHP you can do the following:

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);
+1
source

Source: https://habr.com/ru/post/1755141/


All Articles