将本地上传图片反应为php

时间:2018-11-13 20:00:41

标签: php json react-native server upload

我正在尝试保存从react-native-view-shot返回的图像,看起来像这样(file:///data/user/ReactNative-snapshot-image123.png),但我不知道为什么该图像未保存在我的服务器上。 这就是我所做的。

       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();
            });
        }

这是我的php,用于将图像保存在服务器上。

<?
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";
}
?>

我的PHP仅返回此错误:

  

getimagesize()期望参数1为字符串,给定数组

我尝试使用var_dump($ _ POST)来查看返回了什么,但是我得到了类似的东西

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

您知道我在做什么不对吗? 我也做过

body: data

在没有json.stringify但仍然没有的情况下,我的反应获取。

谢谢!

已解决!如果有人需要

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");

0 个答案:

没有答案