1

I have uploaded an image to a local folder using the below code using ajax and php after some cropping and image zooming functionality.

After this function, the cropped image is sanded to a local folder called upload. But the image is processed with base64 encode and decode model. I want to send the cropped/saved image to an api with out base64 data all I want is send the image as form data/file.

The code that used for image upload is image data is sent as form data and is base64 encoded format

url = URL.createObjectURL(blob);
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function(){
var base64data = reader.result;
formData.append('image', base64data);
$.ajax({
   url:'<?php echo get_template_directory_uri(); ?>/upload.php?>',
   method:'POST',
   data:formData

and in upload.php

if(isset($_REQUEST['image'])) {
    $data = $_REQUEST['image'];
    $image_array_1 = explode(";", $data);
    $image_array_2 = explode(",", $image_array_1[1]);
     $data = base64_decode($image_array_2[1]);
    $image_name = 'upload/' . time() . '.png';
    file_put_contents($image_name, $data);
} 

i want to take this image from folder and send this data to an api as file/multipart formdata with out encryption/decrypted format

Please help me to solve this. Thank you in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
viyan
  • 31
  • 3

1 Answers1

1

Once you have the image on your drive you can use curl with curlFile option for the purpose. Docs for curl and curlFile

From the docs you can see its usage

/**
 * @param string $file -> full file path
 * @return CURLFile
 */
function makeCurlFile($file)
{
    ## Create the curl file for upload
    $mime = mime_content_type($file);
    $info = pathinfo($file);      # or simply use $name = basename($file);
    $name = $info['basename'];
    $output = new CURLFile($file, $mime, $name);
    return $output;
}

$file = '/path/to/your/upload/directory/picture.png';
$ch = curl_init("https://example.com/api_url");
$photo = makeCurlFile($file);

## Construct your post data in an array
$postData = [
    'fileObject' => $photo,                 # fileObject is the name of the file which your api is expecting
    'desc' => 'your custom description'     # Any other field that need to be sent along with the file, use as many needed
];

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    $result = curl_error($ch);
}
curl_close($ch);


Note:

  1. This applies to php >= 5.5.0. For versions prior to that you cannot use curlFile and need to use @ and build the body of the content manually. Read this answer or here to see how its done.
  2. I have not set the Content-Type header. cUrl appends this header automatically as it deems fit. At least in my case whenever we ask curl to post an array it uses the Content-Type multipart/form-data. If there is an issue you will need to check headers are setup correctly.
  3. CURLOPT_SAFE_UPLOAD A comment in the docs (here) suggest we set the CURLOPT_SAFE_UPLOAD to true for all versions >= 5.5. However, the docs is silent on the issue except this page. So I am assuming the default for this for versions >= 5.6 is true and hence you do not need to explicitly set it when using curlFile. For at least php 7.2 I can confirm it works without setting this to true.

Uploads using the @file syntax are now only supported if the CURLOPT_SAFE_UPLOAD option is set to false. CURLFile should be used instead.

  1. Make use of curl_error and curl_getinfo to debug
endeavour
  • 576
  • 4
  • 15