1

I'm trying to save an image returned from react-native-view-shot which looks something like this (file:///data/user/ReactNative-snapshot-image123.png) but I don't know why the image is not saved on my server. This is what I've done.

       savePicture = () =>{
            this.refs.viewShot.capture().then(uri => {
            this.setState(
                {
                imageURI:uri
            });

            var data = new FormData();
            data.append('image', {
              uri: this.state.imageURI,
              name: 'my_photo.png',
              type: 'image/png'
            });
            fetch('http://www.url.com/upload.php', {
                headers: {
                  'Accept': 'application/json',
                  'Content-Type': 'multipart/form-data'
                },
                method: 'POST',
                body: JSON.stringify({
                    image: data
                })
              })
                .then((response) => response.json())
                .then((responseData) => {console.warn(responseData);})
                .catch((error) => {
                    console.warn(error);
                })
                .done();
            });
        }

and here is my php to save the image on my server.

<?
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $shuffledid = str_shuffle(124243543534543534);
    $url = $shuffledid;
    $path = $_SERVER['DOCUMENT_ROOT'].'/android';

    $json = json_decode(file_get_contents('php://input'), true);
    $img = $json['image'];
    list($width,$height,$type) = getimagesize($img);
    switch(strtolower(image_type_to_mime_type($type)))
    {
        case 'image/gif':
            $pic = imagecreatefromgif($img);
            break;
        case 'image/png':
            $pic = imagecreatefrompng($img);
            break;
        case 'image/jpeg':
            $pic = imagecreatefromjpeg($img);
            break;
        default:
            return false;
            break;
    }

    $bg = imagecreatetruecolor(600,700);
    imagecopyresampled($bg,$img,0,0,0,0,600,700,$width,$height);
    imagepng($bg, $path.'/'.$url.'.png');
    imagedestroy($img);

    echo "done";
}
?>

My PHP just returns this error:

getimagesize() expects parameter 1 to be string, array given

I tried with var_dump($_POST) to see what is returned but I got something like this

{line:21352",column:16,sourceUrl:http.../index.delta?platform=android$dev=true...

Do you have any idea what I'm not doing right? I've also did

body: data

on my react fetch without json.stringify but still nothing.

Thank you!

Solved! If someone needs this

const formData = new FormData();
            formData.append('image', {
              uri: this.state.imageURI,
              name: 'my_photo.png',
              type: 'image/png'
            });
            formData.append('Content-Type', 'image/png');

            fetch('http://www.url.com/upload.php',{
                method: 'POST',
                headers: {
                    'Content-Type': 'multipart/form-data',
                  },
                  body: formData
              })
              .then((response) => response.json())
              .then((responseJson) => {
                 console.log(responseJson);     
                })
                .catch((error) => {
                    console.log(error);
                  });
            });

PHP

$img = $_FILES['image']['tmp_name'];
$shuffledid = str_shuffle(124243543534543534);
$url = $shuffledid;
$path = $_SERVER['DOCUMENT_ROOT'].'/android';
list($width,$height,$type) = getimagesize($img);
$pic = imagecreatefrompng($img);
$bg = imagecreatetruecolor(600,700);
imagecopyresampled($bg,$pic,0,0,0,0,600,700,$width,$height);
imagepng($bg, $path.'/'.$url.'.png');
imagedestroy($pic);
echo json_encode("done");
paulv
  • 83
  • 2
  • 12
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Nov 13 '18 at 21:12
  • Look at the JSON you're building on the client side, and then look at what you're trying to extract from it on the server side. They do not match. I don't know react, but it doesn't look like you're sending an image at all. – miken32 Nov 13 '18 at 21:15
  • this.state.imageURI is the image saved on the device cache. – paulv Nov 13 '18 at 23:07
  • But is it an image, or just a URI to an image? – miken32 Nov 13 '18 at 23:08
  • Regardless, you have to get to it first, by making sure your PHP knows the correct format of your JSON, which it does not at the moment. – miken32 Nov 13 '18 at 23:09
  • file:///data/user/ReactNative-snapshot-image123.png This is how it looks like. It’ tmp on the device and it can be used in the app until you close the it – paulv Nov 13 '18 at 23:10
  • Ok, that should be fine to pass to `getimagesize()`. Just have to look at how you're building your JSON object and adjust your PHP code to match. – miken32 Nov 13 '18 at 23:14
  • I passed the image to getimagesize and it returned this "getimagesize("file:\/\/\/data\/user\/0\/cache\/ReactNative-snapshot-image1583725451721380188.png"):failed to open stream:" I need to convert it to base64? – paulv Nov 14 '18 at 10:39

0 Answers0