如何在PHP中检索以JSON格式发送到我的服务器的图像?

问题描述 投票:0回答:1

我想检索从应用程序发送到我服务器上的PHP脚本的临时图像文件。问题是我想采用JSON中path路径变量中声明的图像,并将其用作$_FILES['picture']['name'];等PHP文件,就好像它是从传统的Web表单文件输入发送一样。

PHP代码和JSON在下面,任何帮助都很受欢迎,因为我一直坚持这个!

JSON:

{"_origin":-112,"_subscribers":[{"version":2,"active":true}],"_isProxy":false,"_values":[{"path":"/private/var/mobile/Containers/Data/Application/3EF19C87-AA05-4990-883B-7F569E118105/tmp/images/IMG_4980428D-A13F-4647-98F4-D9C75232D752.jpg","name":"IMG_4980428D-A13F-4647-98F4-D9C75232D752.jpg","width":901,"height":1200,"info":{}}],"_beganSubscriptions":true}

PHP:

<?php

//Grab variables
$pass = $_GET['pass'];
$title = $_POST['name'];
$email = $_POST['email'];
$market = $_POST['market'];
$account = "414890";
$date = date("Y-m-d");

//Decode JSON
$picture = json_decode($_POST['picture'], true);
$name = $picture->{'path'};

if($pass == "MY_KEY") { 

        $name = $_FILES['picture']['name'];
        $size = $_FILES['picture']['size'];
        $type = $_FILES['picture']['type'];

        $tmp_name = $_FILES['picture']['tmp_name'];

        $extension = substr($name, strpos($name, '.') + 1);

        $max_size = 8000000;

        //iOS Image Rotation Fix
        function correctImageOrientation($filename) {
          if (function_exists('exif_read_data')) {
            $exif = exif_read_data($filename);
            if($exif && isset($exif['Orientation'])) {
              $orientation = $exif['Orientation'];
              if($orientation != 1){
                $img = imagecreatefromjpeg($filename);
                $deg = 0;
                switch ($orientation) {
                  case 3:
                    $deg = 180;
                    break;
                  case 6:
                    $deg = 270;
                    break;
                  case 8:
                    $deg = 90;
                    break;
                }
                if ($deg) {
                  $img = imagerotate($img, $deg, 0);       
                }
                // then rewrite the rotated image back to the disk as $filename
                imagejpeg($img, $filename, 95);
              } // if there is some rotation necessary
            } // if have the exif orientation info
          } // if function exists     
        }

        //Unique Image Filename
        $newfilename = round(microtime(true)) . '.' . end($tmp_name);


        //Validate and Post
        if(isset($name) && !empty($name)){
            if(($extension == "jpg" || $extension == "jpeg" || $extension == "png" || $extension == "JPG" || $extension == "PNG" || $extension == "JPEG") && $extension == $size<=$max_size){
                $location = "../member-images/";
                correctImageOrientation($tmp_name);
                if(move_uploaded_file($tmp_name, $location.$newfilename)){



                    // Insert Info into Database
                    $mysqli->query("INSERT INTO voting (account, date, title, market, image) VALUES ('$account', '$date', '$title', '$market', $newfilename')");

                    /* Insert Info into Database
            $mysqli->query("INSERT INTO voting (account, date, title, phone, carrier, note, market, image) VALUES ('$account', '$date', '$title', '$cleaned_phone', '$carrier', '$note', '$market', '$newfilename')"); */


                }else{
                    echo 'Failed to Upload Photo. Please click back and try again.';
                }
            }else{
                echo 'File size should be no more than 100 KiloBytes & Only JPEG, JPG or PNG File. Please click back and try again.';
            }
        }else{
            echo 'Please click back and be sure to select a photo file.';
        }

        // close connection 
        $mysqli->close();


} else {

    die();

}


?>
javascript php json
1个回答
-1
投票

您可以从JSON中提取文件路径并将其移动或复制到您想要的位置:

$picture = json_decode($_POST['picture'], true);
$name = $picture->{'path'};

复制:

copy($name, $tmp_name, $location.$newfilename);

移动:

rename($name, $tmp_name, $location.$newfilename);
© www.soinside.com 2019 - 2024. All rights reserved.