I'm creating an Ionic app and I'm using a PHP server in order to upload the pictures taken with my mobile device to my server. The PHP API that I'm using it's already developed in prod and working without any issues on my web version but I'm having problems when I try to use the same POST request.
My Ionic code:
upload_image():Observable<any>{
//This is not the complete string.
let _image64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGC";
let params = `image64_str=${_image64}`;
let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
let httpOptions = {headers};
return this.http.post<any>("http://API_URL",params,httpOptions);
}
My PHP code:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header('Content-type: application/x-www-form-urlencoded');
$strimg = $_POST['image_str64'];
$image_str = substr($strimg ,strpos($strimg , ',')+1); //To avoid "data:image/jpeg;base64," from the 64string
$data = base64_decode($image_str);
//
//rest of code to check the format, directory and so one.
//
The problem is that when I'm calling this API from my Ionic function, I'm getting the following error:
<b>Warning</b>: imagecreatefromstring(): gd-jpeg: JPEG library reports unrecoverable error: in <b>/API/test.php</b> on line <b>57</b>
<b>Warning</b>: imagecreatefromstring(): Passed data is not in 'JPEG' format in <b>/API/test.php</b> on line <b>57</b>
<b>Warning</b>: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in <b>/API/test.php</b> on line <b>57</b>
The "interesting point" is that if I "hardcode" the same string base64Data taken from Ionic app into my PHP file, the upload works without any problem. Also I have checked that the string send from my Ionic app is exactly the same text that I receive into my PHP file throught $_POST['varialbe'] option.
Can you please let me know if there is any eror within my headers/functions or if I should try to use another way to upload my file?
Thank you in advance