How to upload raw bytes of image data to facebook

The program will load the user profile image and then apply the effect to it through imagemagick. Since the facebook example is intended only for the file base, and I do not want to store it as a temporary file, how to upload a finalized binary image to facebook

<?php 
  //1. prepare frame
  $overlay = new Imagick('1.png');

  //2. prepare base image
  $base = new Imagick();
  $base->readImageFile(fopen('https://graph.facebook.com/'.$id.'/picture?type=large', 'rb'));


  //3. finalize the image
  $base->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 0, 0);


  //4. prepare form data
  $options = array(
    CURLOPT_URL         => "https://graph.facebook.com/me/staging_resources",
    CURLOPT_POST        => true,
    CURLOPT_HTTPHEADER  => Array("Content-Type: multipart/form-data"),
    CURLOPT_POSTFIELDS  => array(
      'file' => 'data:image/png;base64,'.base64_encode($base),
      'access_token' => ''
    )
  );

  //5. send to staging_resources 
  $ch = curl_init(); 
  curl_setopt_array($ch, $options);
  echo curl_exec($ch);

  //6. create og object

?>
+1
source share

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


All Articles